本帖最后由 qwert 于 2012-3-5 23:19 编辑
二维数组转换成一维数组,是这样么?- class ArrayTest
- {
- public static void main(String[] args)
- {
- int[][]arr={{4,5,6},{8,3,1,2,7},{4,2,8,11,9,1},{6,3,2,4,7},{2,2}};
- ArrayCount(arr);
- }
- static void ArrayCount(int[][] arr)
- {
- int count=0;
- int pos=0;
- for (int x=0;x<arr.length ;x++ )
- {
- for (int y=0;y<arr[x].length ;y++ )
- {
- count++;
- }
- }
- int[] arr1=new int[count];
- for (int x=0;x<arr.length ;x++ )
- {
- for (int y=0;y<arr[x].length ;y++ )
- {
- arr1[pos++]=arr[x][y];
- }
-
- }
- System.out.print("[");
- for (int x=0;x<arr1.length;x++ )
- {
- if(x!=arr1.length-1)
- System.out.print(arr1[x]+",");
- else
- System.out.println(arr1[x]+"]");
- }
-
-
- }
- }
复制代码 |