我必须输出每行最多 4 个数组值的数组,但我不知道如何将其转换为二维数组。破折号之后是我遇到麻烦的地方。如果我不将其输出为 2D 数组,我还能如何将其限制为每行只有 4 个值?
public class arrayExampleB{
public static void main(String[] args){
int[] x = {22, 12, 28, 4, 30, 59, 17, 82, 1, 99, 47, 2, 8, 20, 80};
System.out.print("Pre-Swapped Array Set (linear): {");
for(int i=0; i<=x.length-1; i++){
if(i<x.length-1){
System.out.print(x[i] + ", ");
}
else{System.out.print(x[i]);}
}
System.out.print("}");
int y = x.length-1;
int temp = x[y];
x[y] = x[1];
x[1] = temp;
int z = x.length-2;
int temp2 = x[z];
x[z] = x[0];
x[0] = temp2;
System.out.print("\nPost-Swapped Array Set (linear): {");
for(int i=0; i<=x.length-1; i++){
if(i<x.length-1){
System.out.print(x[i] + ", ");
}
else{System.out.print(x[i]);}
}
System.out.print("}");
//------------------------------------------------ -------------
int d = (x.length / 4) + (x.length % 4);
int i = 0;
int j = 0;
int[][] t = new int[i][j];
System.out.print("\nPre-Swapped Array Set (2D): {");
for(i=0; i <= 4; i++){
for(j=0; j < d; j++){
System.out.print(t[i][j] + " ");
}
System.out.println();
}
System.out.print("}");
}
}