• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Multidimensional Arrays in Java

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure how to visualize Multidimensional arrays in Java.

For example:



Does that mean that you have 5 references that can hold array of 2 strings ?

 
Marshal
Posts: 64638
225
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They aren't actually multipdimensional arrays, but array of arrays. Because you gave each of your included arrays a size they aren't null. What you now have is more like this:-If you want each of the elements to be null, you can probably say new Foo[5][]
 
Bartender
Posts: 5902
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Losty wrote:

When initialized in that way, it holds an array of arrays of 2 Strings.

It  is similar to:
Note that the square brackets should be after the type name and not after the variable name.
 
John Losty
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will add another 2 lines of code:



But instead of compilation error it probably does this:



Am I correct?
 
Carey Brown
Bartender
Posts: 5902
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct. Technically Java does not have multi-dimensional arrays, it has arrays of arrays, where each subarray could be a different length or even a null place holder for a subarray.

Now you have an array that holds 5 arrays (as not yet constructed, initially nulls), where each subarray would be a String[] of any length.
Then you can have:
FIX your bracket placement or the Java Syntax Conventions Police will get you.
 
Carey Brown
Bartender
Posts: 5902
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Typically, if you have arrays-of-arrays declared in your code, then you will have a nested loop to traverse them. This is where you need to be careful in your loop constructs to allow for varying lengths of arrays.
 
Campbell Ritchie
Marshal
Posts: 64638
225
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Losty wrote:. . . Am I correct?

If you mean can you get the same result in one stage or two stages like that, then you are correct. If you had a true multidimenstional array, you would not get such code to compile,, because the included arrays would be contrainsed to be the same size (2).
 
John Losty
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot edit the declaration in the first post.
 
Sheriff
Posts: 6029
157
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And you shouldn't.  It will make the following posts look inaccurate.  Instead, post a new reply.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!