我想檢查一個重複項是否存在於一個2D數組的行中並將其替換。檢查在java行中只有一個2d數組中的重複項
因此,如果數組是:
7, 5, 6, 1, 7, 5, 7, 1 // would change the 7 in [4] and [6]
2, 4, 3, -1, 7, 5, 8, 3 // would change the 3 in [7]
8, 7, 2, -3, 7, 1, 5, 1
5, 7, 3, 2, 4, 5, -4, 8
6, 1, 8, 2, 2, 6, 1, 3
int row = 0;
if (row < zipcodelist.length) {
for (int z = 0; z < zipcodelist[row].length; z++) {
for (int y = 1; y < zipcodelist[row].length; y++) {
if (zipcodelist[row][z] == zipcodelist[row][y]) {
zipcodelist[row][y] = 1 + generator2.nextInt(8);
} else if (zipcodelist[row][z] != zipcodelist[row][y]) {
System.out.println("Not duplicate");
}
}
}
row++;
}
但有了這個代碼,一切都在變化,而不僅僅是重複。我究竟做錯了什麼?
對不起,這個generator2.nextInt是要生成一個隨機數,這樣做的代碼是: Random generator2 = new Random(); – user3369920
我不明白你想要什麼 –