0

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
}
^
3
  • 1
    try: k[0] = new int[]{1}; k[1] = new int[]{2,3}; Commented Mar 27, 2014 at 5:26
  • @YohanesKhosiawan许先汉 you should put that as an answer. and explain it Commented Mar 27, 2014 at 5:27
  • i see, OP, if you don't mind, please accept my answer below.. Commented Mar 27, 2014 at 5:28

3 Answers 3

2

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};
Sign up to request clarification or add additional context in comments.

Comments

0

You cannot use in this way, that is hot java syntax is defined.

 k[0] = {1};

You can only do so while you are initializing the array, like this, for example :

 int[][] k = {{1},{2,3}};

For examples, you look here

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.