1

I know that the array declaration below is correct:

String[][] arrayNew={{"teresa","human","jennifer"},{"18","20"},{"nothing"}};

I understand that String and array are two different object types in java.So how come the string array arrayNew is successfully storing three different arrays in it. An int array can not store doublesbut how come a string array is able to store arrays?

6
  • 1
    How come arrayNew is a string array? its an array of an array of string man. Commented Dec 27, 2015 at 16:51
  • @SazzadHissainKhan Got it now.I was confused probably. Commented Dec 27, 2015 at 16:53
  • Okay.It was actually a problem with the basics.My teacher had asked us to consider a 2D array as a matrix and this made it confusing.But now it's pretty clear. Commented Dec 27, 2015 at 17:05
  • 2
    great to here u got it :) Commented Dec 27, 2015 at 17:06
  • Please don't edit your question in a way which invalidates posted answers. If you have additional question ask them in new post. Commented Dec 27, 2015 at 17:23

7 Answers 7

4

You can think of Type[] as one-dimensional array which can store elements of described Type. So Type[] array can look like {Type0, Type1, Type2}.

Now lets say that we want to create array which can store 1D arrays. How should we declare it? Our Type will need to be replaced with String[] which will give us

String[][] arrayNew
^^^^^^^^
type of elements which can be stored directly in `arrayNew`

So since Type is String[], it means that arrayNew is not String array, but array of String arrays

{    [0]        [1]        [2]   }
      ↓          ↓          ↓    
  String[],  String[],  String[]
  //each of these 1D array can store its own elements.
Sign up to request clarification or add additional context in comments.

3 Comments

That means 2D arrays and above can only store arrays(of the same type)...am I right?
Yes, 2D arrays like arrayNew can store only 1D String arrays, and these 1D arrays can store Strings.
I have edited the above question to ask one more doubt which emerged from the previous one.Can you help me with it?
3

You are just creating a 2D array (An array of String array).

On this particular case, you created a jagged array.

You could do the same for int array as well:

int[][] array = {{1,2,3}, {4,5}, {6}};

Multidimensional arrays are array of arrays:

String[] arrayA;       //Array of Strings
String[][] arrayB;     //Array of (Array of Strings)
String[][][] arrayC;   //Array of (Array of (Array of Strings))
String[][][][] arrayD; //Array of (Array of (Array of (Array of Strings)))

9 Comments

I found this pretty useful.Thanks a lot!
@MathewsMathai You are welcome, but I don't know why people somehow down voted it.
Your code wouldn't compile, and thus makes things more confusing than helpful: you can NOT store arrays of ints in an int[], as your code suggests.
@JBNizet Ah yes, I missed out the bracket. Now it is compliable.
@MathewsMathai I was reading it, but it was gone. Maybe you want to start a fresh question? I will help you take a look at it.
|
2

arrayNew is not a string array (String[]) it's an array of arrays of strings (String[][]). As such, each of its elements is an array of strings - {"teresa","human","jennifer"} (an array of strings with three elements) , {"18","20"} (an array of strings with two elements) and {"nothing"} (an array of strings with a single element).

Comments

2

Since it's declared as String[][] with two [] this means it's a two-dimensional array.

In other words:
String - can contain only one string
String[] - can contain an array of strings (multiple strings)
String[][] - can contain an array of arrays of strings (multiple arrays of multiple strings)
... and so on

Comments

1

In this case the string array can hold multiple arrays since its a Two Dimensional Array. Here is a documentation about it.

Integers can also do that as the following:

int[][] myArray = {  {0, 1, 2, 3},
                     {3, 2, 1, 0},
                     {3, 5, 6, 1},
                     {3, 8, 3, 4}  };

Comments

1

String[] is an array of Strings.

Foo[] is an array of Foos.

Bar[] is an array of Bars.

So, String[][] is an array of String[]s. I.e. an array of arrays of Strings. That's why it can store arrays of Strings.

3 Comments

That means 2D arrays and above can only store arrays(of the same type)...am I right?
I would think in terms of 2D or 3D arrays. Just in terms of arrays (which is the reality, BTW). String[][] is an array of String[], which can thus store String[] instances. Just like Banana[] is an array of Bananas that can store Banana instances. And Foo[][][] is an array of Fo[][] that can store Fo[][] instances.
What I meant is I would NOT think in terms of 2D or 3D arrays.
1

arrayNew is a reference to an 2d array of type String and all the elements are also of string type, so it compiles.

ex: String[] arr = {"abc","xyz"};, arr is referring to an array of type String ,arr is not the array itself.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.