数组按行/列优先存储转换
package com。wrh.lab.dataStructure.arrayAndGenericTable;
/**
* test the array convert
* @author wrh
*
*/
public class ArrayConvert {
public static void main(String[] args) {
int[][] data = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
int[] rowData = new int[12];
int[] colData = new int[12];
int i,j;
System.out.println("Out put the two dimension array:");
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++) {
System.out.print(" " + data[i][j] + " ");
}
System.out.println();
}
System.out.println("*********convert to row first store");
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++) {
rowData[i * 4 + j] = data[i][j];
}
}
for(i = 0; i < rowData.length; i++) {
System.out.print(" " + rowData[i] + " ");
}
System.out.println();
System.out.println("******convert to column first store");
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++) {
colData[j * 3 + i] = data[i][j];
}
}
for (i = 0; i < 12; i++) {
System.out.print(" " + colData[i] + " ");
}
}
}
跟我走啊~~