Just copy and see the effect to understand
package testJava;
public class 一维数组映射为二维数组 {
public static void main(String[] args) {
//创建一维数组,并赋值。
int data[]=new int [50];
for(int i=0;i<50;i++) {
data[i]=i+1;
}
//输出数组,每输出5个后换行
int conter=0;
for (int i = 0; i < data.length; i++) {
conter++;
System.out.print(data[i]+" ");
if(conter==5) {
conter=0;
System.out.println();
}
}
//syso输出分割线----------
System.out.println("--------------------");
//创建二维数组,10行5列
int [][] da=new int[10][5];
one2Two(data,da);
// 输出二维数组
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(da[i][j]+" ");
}
System.out.println();
}
}
/*!!!注意:
* 1.传入的数组里两个数组的大小(一维数组length为10,则二维数组的行数乘列数也为10
* 2.数组类型必须一样
* */
public static void one2Two(int []data,int [][] da) {
int k=0;
int hang=da.length;
int lie=da[0].length;
for(int i=0;i<hang;i++) {
for(int j=0;j<lie;j++) {
da[i][j]=data[k];
k++;
}
}
}
}
Operation effect (below):
![](https://img-blog.csdnimg.cn/2019101121295376.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTk2NzYwMA==,size_16,color_FFFFFF,t_70)