Arvind
Arvind

Reputation: 58

Error while testing question on Java multidimensional array

I was coding some sample examples of Java questions on Multi-dimensional arrays to get an understanding on its initialization and accessibility. I copied the code syntax as in the example but am not able to understand the how to decode the code.

Following the example, I tried to get an understanding on the initialization of the multi-dimensional array. I couldn't grasp the syntax logic and made just one change to the code to test my understanding, the example was initially to get the output of String value = exampleStr[0];. I changed it to String value = exampleStr[0][0]; which gave out the error.

public class MyClass {
    public static void main(String args[]) {
      String exampleStr[] = new String[][] 
      {
          {null,null,null}, 
          new String[] {"a","b","c"}, // not sure what this mean
          {new String()} // not sure of this too

      }[0]; 
// what does the whole block actually mean in a compiler/logic sense ?

      String exampleStr3[] = {null};

// this is where the compiler error came!
      String value = exampleStr[0][0];

// shows 3 for this code when error line is commented out
      System.out.println(exampleStr.length); 

      System.out.println(value);


    }
}

In the example, the syntax is String value = exampleStr[0]; which outputs null, I changed it to String value = exampleStr[0][0]; and got the error. Why am I getting the error and could someone explain to me the behavior of the code/compiler in regards to this code's syntax. How is the array structure

Upvotes: 2

Views: 328

Answers (3)

azro
azro

Reputation: 54148

Your initial code meant that your create a 2-dimensionnal array but only keep the first box (using the [0] at the end), so you could read its first element with exampleStr[0] but exampleStr[0][0] is not possible because exampleStr[0] is not an array, so you can't access something into it using an index

String exampleStr[] = new String[][] 
  {
      {null,null,null}, 
      new String[] {"a","b","c"}, 
      {new String()} 
  }[0];

If we split it, it look slike

String array[][] = new String[][] 
  {
      {null,null,null}, 
      new String[] {"a","b","c"}, 
      {new String()} 
  };
String exampleStr[] = array[0]; // which is the {null,null,null} part

Details for the 2-dim array and the details into it:

First, to get it working, exampleStr should be a 2-dimensionnal array with [][]

So there double [] so it's a 2 dimensionnal array of type String, which means, that you have an outer array, and each box can contain an (inner) array of String

Here you have, for each box of the outer array:

  1. {null, null, null} an array of three times the null element
  2. new String[]{"a", "b", "c"} an array of 3 String, can be reduced in {"a", "b", "c"}
  3. {new String()} an array of one empty String, same as {""}

    String[][] exampleStr = new String[][]{
            {null, null, null},
            new String[]{"a", "b", "c"}, // not sure what this mean
            {new String()} // not sure of this too
    };
    

Then exampleStr[0][0] means accessing the first element of the first box

Upvotes: 1

davidrhp
davidrhp

Reputation: 125

From my understanding, you are first declaring a 1-dimensional array exampleStr[]. Then, you are creating a 2-dimensional array, but are subscripting it with a 0, i.e., you are selecting the array at index 0, the array containing the null elements.

exampleStr[0][0] => with the first [0] you are selecting the first element at index 0, a null type. The second [0] would be called on that element, which is a null type and therefore not valid.

Essentially, you are not creating a 2-dimensional array in the first place.

Change your code to this and it will work:

public static void main(String args[]) {
    String exampleStr[][] = new String[][]
            {
                    {null,null,null},
                    // creates a 1-d String[], -> {"a", "b", "c"} is enough
                    new String[] {"a","b","c"},
                    // creates an array containing an empty string, -> {""} would be equivalent
                    {new String()} 

            };

    String exampleStr3[] = {null};

    String value = exampleStr[0][0];

    System.out.println(exampleStr.length);

    System.out.println(value);
}

EDIT: I added answers to the questions in the comments of the above code segment.

Upvotes: 1

Jonathan Fetzer
Jonathan Fetzer

Reputation: 1

Wow, that's a weird example, where did you find such a thing? I believe what is going on is that exampleStr is actually a one-dimensional array, so there is no exampleStr[0][0].

To start with there are not really multi-dimensional arrays in Java as there are in languages like C#, but there are arrays of arrays. The curly bracket syntax is a way of initializing a data structure.

String exampleStr[] = new String[][] // This declares an array of arrays.
      {
          {null,null,null}, // first column values
          new String[] {"a","b","c"}, // second column values
          {new String()} // empty string is only element added to third column

      }[0]; // This initializes exampleStr to the first column values above.

exampleStr is a one-dimensional array of length 3, whose elements are null, null, and null. You would access those values as exampleStr[0], exampleStr[1], and exampleStr[2]. Hope this helps!

Upvotes: 0

Related Questions