我想遍历给定顶点集合中的所有顶点并比较两个对之间的距离。假设我的顶点名单如下:比较java中的顶点对
(1,2),(3,4),(5,6),(7,8),(9,10),(11,12)
我要遍历以下列方式顶点集:
第一次迭代:
for pair (1,2) and (3,4) I want to compare it with [(5,6),(7,8)],[(7,8), (9,10)]and[(9,10),(11,12)]
第二次迭代:
for pair (3,4) and (5,6) I want to compare it with [(7,8),(9,10)]and[(9,10),(11,12)]
第3次迭代:
for pair (7,8) and (9,10) I want to compare it with [(9,10),(11,12)]
我该如何在java中使用嵌套循环?我想将顶点集存储在一个数组列表中。我曾尝试通过以下方式,但得到的异常:
for(i=0;i<arraylist.size();i++)
{
for(j=i+2;j<arraylist.size();j++
{
//x,y,u,v are objects of the vertex class
x=arraylist.get(i);
y=arraylist.get(i+1);
u=arraylist.get(j);
v=arraylist.get(j+1);
}
}
我需要什么额外的变化在上面的代码片段遍历顶点集正如我上面解释做什么呢?
而你会得到的例外是什么?或者它是一个错误,因为你的第二个for循环缺少一个尾部圆括号? –
线程“main”中的异常java.lang.IndexOutOfBoundsException:索引:20,大小:20 \t at java.util.ArrayList.rangeCheck(ArrayList.java:635) \t at java.util.ArrayList.get(ArrayList。 java:411) @ Jonny Henly – user6216509