Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array initialization

 
Rizwan Qadri
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all,

How can we initialize or use this array.

class ArrayTest
{
public static void main(String[] args)
{
int[][] values =new int[2][];
System.out.println(values[1]);
}
}

Output- null
I cant understand.
 
Jan van Mansum
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"values" is an array of length 2 with elements of type "array of int". It is initialized to the value {null, null}, because the length of the arrays that are the elements of "values" is unknown.

Compare what happens if you declare "values" like this:

Now the JVM will initialize the two elements of "values" to an int array with 3 elements: {{0, 0, 0},{0, 0, 0}}.

If you want to print the elements of the array, you need to access them separately:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic