• 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
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

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: 65814
250
  • 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][]
 
Saloon Keeper
Posts: 6243
58
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
Saloon Keeper
Posts: 6243
58
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
Saloon Keeper
Posts: 6243
58
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: 65814
250
  • 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: 6268
167
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.
 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!