2

I have a brief question about how Java handles arrays. Below is my code:

//import java.util.Arrays;
import static java.lang.System.out;

public class Arrays
{       
    public static void main(String[] args)
    {           
        String [][] multiArray = new String[10][8];

        int k = 1;
        while (k <= 61) {out.print('-'); k++;}
        out.println ();

        for (int i = 0; i < multiArray.length; i++)
        {
            for (int j = 0; j < multiArray[i].length; j++)
            {
                multiArray[i][j] = i + "" + j;
                out.print ("| " + multiArray[i][j] + " ");  
            }
            out.println ("|");
        }           
        k = 1;
        while (k <= 61) {out.print('-'); k++;}
        out.println();          
    }       
}

I understand that you have to create a double "for" loop to print out values for both dimensions and that you have to have:

    multiArray[i].length

so that it knows to reference the length of the second dimension. I just don't understand how it works.

What I'm confused about is this: At the very beginning of the program, directly after I declare my array, if I write a statement like:

    system.out.println (multiArray.length);    

It will print the value of 10, which is the length I declared in the first dimension. If I, however, create some random variable like "int a = 0" or "int idontgetthis = 0" and then I write:

    system.out.println (multiArray[a].length);

it somehow knows to print the length of the second dimension, 8. So my question is, how does it know how to do this? It's killing me!! lol

0

3 Answers 3

5

Because multiArray is really an array of arrays. So multiArray[a] is a reference to an object. That object is itself an array. That array has a length (8), and a property called length which can be used to return that length.

Sign up to request clarification or add additional context in comments.

1 Comment

An array of arrays? Gaahhhh lol I'm trying my best to understand. Thanks for the hasty response!
0

Basically, it is a concept confusion, by doing:

String[] array;

you are declaring that you will have an array of Strings with an unknown lenght.
A call to: System.out.println(array.length) at this moment will fail with a compilation error because array is not yet initialized (so the compiler can't know how long it is).

By doing:

String[] array = new String[8]

you declare that you will have and array of String and initialize it, specifying it will have space for 8 Strings, the compiler then allocates space for this 8 Strings. Something important to notice is that even when the compiler now knows that you will store 8 Strings in your array, it will fill it with 8 nulls.

So a call to System.out.println(array.length) at this point will return 8 (Compiler knows the size) but a call to System.out.println(array[1]) will return a Null Pointer Exception (You have 8 nulls in it).

Now, in the example you presented, you are declaring a bidimensional array, this is, an array that will contain other arrays.

Bidimensional arrays are initialized as String[][] multiarray = new String[10][8]; and the logic is the same as in simple arrays, the new String[10][8]; indicates the lenght of the array that contains the other arrays, and the new String[10][8]; indicates the length of the contained arrays.

So doing system.out.println(multiArray[x].length); after initializing multiarray is translated as "What is the length of the Xth contained array?", which the compiler, thanks to your initialization, now knows is 8 for all the contained arrays, even when they are full of nulls at the moment.

Hope it helps to add a bit more understanding!

1 Comment

Ah haaaa! Not crystal clear yet, but that is definitely starting to make more sense. I suppose I'm used to thinking about array the way that FORTRAN and C store them...as "x,y" pairings. It's a more mathematical way of thinking about it to me which makes more sense, but oh well. Thank you!
0

You could try looking at it like this.

public class Arrays{
  public static class EightStrings {
    public String[] strings = new String[8];
  }
  EightStrings[] tenOfThem = new EightStrings[10];
}

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.