JAVA一维数组变二维数组问题14
发布网友
发布时间:2023-10-11 00:30
我来回答
共1个回答
热心网友
时间:2024-11-25 04:22
下面这个程序做到了:
public class Test1 {
public static void main(String[] args) {
int a [] = new int[20];
for (int i = 0; i < a.length; i++) {
a[i] = i;
}
int n = 5;//列
int m = a.length / n;//行
int[][] b = new int[m][n];
int num = -1;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
num++;
b[i][j] = a[num];
}
}
System.out.println("原来的一维数组是:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
System.out.println("转换后的二维数组是:");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.print(b[i][j] + " ");
}
System.out.println();
}
}
}