这里我声明了两个数组列表两个相等的数组列表,但Java没有看到等于
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));
其明显的,那些2名数组列表是相同的,但是当我运行这个检查它似乎总是失败。
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;
}
使用equals,而不是'==' –
在哪个库中定义了'arrayListsEqual'? –
@Duncan它在由课程 – user3567826