为什么我的2D阵列无法运行?

时间:2014-12-06 23:34:04

标签: java arrays output

我有一个简单的2D数组正在打印到屏幕上。

我的问题是代码无法正常编译,我做错了什么?

我的非工作代码是:

/* This program initialises two-dimensional arrays.......*/

/* ********************************************************/



 public class InitArray4
{
     // create and output two-dimensional arrays
     public static void main( String[] args )
{

int[][] array1 = { { 1, 2, 3 }, { 4, 5, 6 } };
int[][] array2 = { { 1, 2 }, { 3 }, { 4, 5, 6 } };



System.out.println( "Values in array1 by row are" );
outputArray( array1 ); // displays array1 by row

System.out.println( "\nValues in array2 by row are" );
outputArray( array2 ); // displays array2 by row
} // end main

// output rows and columns of a two-dimensional array
 public static void outputArray()
 {
 // loop through array's rows
 for ( int row = 0; row < array.length; row++ )
  {
    // loop through columns of current row
    for ( int column = 0; column < array[ row ].length; column++ )

    System.out.printf( "%d ", array[ row ][ column ] );
    System.out.println(); // start new line of output
  } // end outer for


 } // end method outputArray
} // end class InitArray

1 个答案:

答案 0 :(得分:1)

只需在函数中添加参数

 public static void outputArray( int[][] array) {