【发布时间】:2014-04-24 08:07:45
【问题描述】:
这里我声明了两个数组列表
ArrayList<Location> possibleMoves = new ArrayList<Location>();
possibleMoves.add(new Location(0, 1));
possibleMoves.add(new Location(1, 0));
ArrayList<Location> possibleMoves1 = new ArrayList<Location>();
possibleMoves1.add(new Location(0, 1));
possibleMoves1.add(new Location(1, 0));
很明显这两个数组列表是相同的,但是当我运行这个检查时,它似乎总是失败。
if(possibleMoves == possibleMoves1){
System.out.println("lol");
}
我也试过了,失败了
assertTrue("Player 1 1st piece could play to the left and down!",
arrayListsEqual(possibleMoves, possibleMoves1));
这是arrayListsEqual的方法
private boolean arrayListsEqual(ArrayList<Location> a, ArrayList<Location> b) {
if (a.size() != b.size()) {
return false;
}
int size = a.size();
boolean thisOneFound = false;
for (int i = 0; i < size; i++) {
thisOneFound = false;
for (int j = 0; j < size; j++) {
if (a.get(i).equals(b.get(j))) {
thisOneFound = true;
break;
}
}
if (!thisOneFound) {
return false;
}
}
return true;
}
【问题讨论】:
-
arrayListsEqual定义在哪个库中? -
@Duncan 它在课程教授制作的测试文件中给出
-
@user3567826 请告诉我们
arrayListsEqual的定义。 -
@Duncan 我已将其添加到帖子中
-
@user3567826 有趣 - 相等方法不关心列表中项目的顺序。多么奇怪。
标签: java eclipse arraylist junit