public class cs {
public static void main(String[] args) {
int arr1[] = {11, 22, 33, 44, 55};
int arr2[] = {11, 22, 33, 44, 55, 66};
int arr3[] = {11, 22, 33, 44, 55, 77, 88};
int arr4[] = {11, 22, 33, 44, 55, 99, 11, 44, 55};
int arr0[][] = new int[4][];
arr0[0] = arr1;
arr0[1] = arr2;
arr0[2] = arr3;
arr0[3] = arr4;
for (int i = 0; i < arr0.length; i++) {
System.out.print("[");
for (int j = 0; j < arr0[i].length; j++) {
if (arr0[i][j] == arr0[i][arr0[i].length - 1]) {
System.out.print(arr0[i][j] );
} else
System.out.print(arr0[i][j]+ ",");
}
System.out.print("]");
System.out.println();
}
}
}
为什么打印出来会是这样啊
第四个数组55出现了2次。所以判断错误了,应该用索引判断,而不是值
// 判断值 X
if (arr0[i][j] == arr0[i][arr0[i].length - 1]) {}
// 判断索引 √
if (j == arr0[i].length - 1) {}
你的判断只是打不打“,”,“[”和“]”实在外循环中打印的