Reputation: 11
I was writing this class for some testing purpose.
public class Crap {
public static void main(String[] args) {
int[][] k = new int[2][];
k[0] = {1};
k[1] = {2,3};
System.out.println(k[0][0]);
System.out.println(k[0][1]);
System.out.println(k[1][0]);
System.out.println(k[1][1]);
}
}
I am getting the following error while compiling.
Crap.java:5: error: illegal start of expression
k[0] = {1};
^
Crap.java:5: error: not a statement
k[0] = {1};
^
Crap.java:5: error: ';' expected
k[0] = {1};
^
Crap.java:6: error: ']' expected
k[1] = {2,3};
^
Crap.java:6: error: ';' expected
k[1] = {2,3};
^
Crap.java:6: error: illegal start of type
k[1] = {2,3};
^
Crap.java:6: error: <identifier> expected
k[1] = {2,3};
^
Crap.java:6: error: ';' expected
k[1] = {2,3};
^
Crap.java:6: error: illegal start of type
k[1] = {2,3};
^
Crap.java:6: error: <identifier> expected
k[1] = {2,3};
^
Crap.java:6: error: ';' expected
k[1] = {2,3};
^
Crap.java:7: error: <identifier> expected
System.out.println(k[0][0]);
^
Crap.java:7: error: ']' expected
System.out.println(k[0][0]);
^
Crap.java:7: error: ')' expected
System.out.println(k[0][0]);
^
Crap.java:7: error: ']' expected
System.out.println(k[0][0]);
^
Crap.java:7: error: illegal start of type
System.out.println(k[0][0]);
^
Crap.java:7: error: <identifier> expected
System.out.println(k[0][0]);
^
Crap.java:8: error: <identifier> expected
System.out.println(k[0][1]);
^
Crap.java:8: error: ']' expected
System.out.println(k[0][1]);
^
Crap.java:8: error: ')' expected
System.out.println(k[0][1]);
^
Crap.java:8: error: ']' expected
System.out.println(k[0][1]);
^
Crap.java:8: error: illegal start of type
System.out.println(k[0][1]);
^
Crap.java:8: error: <identifier> expected
System.out.println(k[0][1]);
^
Crap.java:9: error: <identifier> expected
System.out.println(k[1][0]);
^
Crap.java:9: error: ']' expected
System.out.println(k[1][0]);
^
Crap.java:9: error: ')' expected
System.out.println(k[1][0]);
^
Crap.java:9: error: ']' expected
System.out.println(k[1][0]);
^
Crap.java:9: error: illegal start of type
System.out.println(k[1][0]);
^
Crap.java:9: error: <identifier> expected
System.out.println(k[1][0]);
^
Crap.java:10: error: <identifier> expected
System.out.println(k[1][1]);
^
Crap.java:10: error: ']' expected
System.out.println(k[1][1]);
^
Crap.java:10: error: ')' expected
System.out.println(k[1][1]);
^
Crap.java:10: error: ']' expected
System.out.println(k[1][1]);
^
Crap.java:10: error: illegal start of type
System.out.println(k[1][1]);
^
Crap.java:10: error: <identifier> expected
System.out.println(k[1][1]);
^
Crap.java:12: error: class, interface, or enum expected
}
^
Upvotes: 0
Views: 453
Reputation: 2449
you need to initialize the second dimension arrays.. and the syntax is as the following:
k[0] = new int[]{1};
k[1] = new int[]{2,3};
Upvotes: 2
Reputation: 70899
Arrays in Java are objects, just not objects with a standard object syntax.
As a result, you must allocate them, and to do so you need to use the new operator.
int[] array = new int[] { 1, 4, 6, 3};
or for assigning an array to an element in a two dimensional array (as you are doing)
array[3] = new int[] { 2, 4, 6, 8 };
Also keep in mind that since they are objects, and since you are using ragged arrays, your call to de-reference the position (0,1) or (0)(1) will fail because the array at position (0) only has an item in the 0 index.
Upvotes: 0