花间一壶酒

导航

数组按行/列优先存储转换

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] + " ");
}

}
}

posted on 2012-02-16 17:23  wrh526  阅读(499)  评论(0编辑  收藏  举报