I have learnt in Java that we can set the size of an array manually in 2D array .
我正在尝试垂直打印数组元素。 此代码给出了运行时错误 这段代码有什么问题?
public class printingarrayvertically
{
public static void main ( String abc [] )
{
int ar[][] = new int [4][] ; // allocates first element of each row .
int i ;
ar[0][0] = 1 ;
ar[1][0] = 2 ;
ar[2][0] = 3 ;
ar[3][0] = 4 ;
System.out.println(" ARRAY ELEMENTS ARE : \n " );
for (i=0 ; i<=3 ; i++)
{
System.out.println( ar[i][0] ) ;
}
}
}
答案 0 :(得分:0)
在第五行,
int ar[][] = new int [4][];
您编写的2D数组将有4行,但您还没有提到有关列的信息。