[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

simple multi dimensional array

Hi,
I am trying to create a multidimensional array. I cant get the following code to work.
I have read lots on the search engines about 2 dimensional arrays, but there doesnt seem to be anything about more than 2. The only closest I got was mulitarrays....
What I am trying to do is:
For example:
String person [] [] [] = { { carl, USA, 43}, {Sonia, Aus, 36}, {mark, GB, 22} };

Does java not support anything larger?
Cheers
john


0
jdav3579
Asked:
jdav3579
  • 8
  • 8
  • 5
  • +2
2 Solutions
 
DeanHorakCommented:
Sure, You can go many dimensions (I don't know what the limit is)...

i.e. a 3-dimensional array
         String[][][] multiArray = {{{"A1","B1"},{"C1","D1"}},{{"A2","B2"},{"C2","D2"}},{{"A3","B3"},{"C3","D3"}},{{"A4","B4"},{"C4","D4"}}};
0
 
CEHJCommented:
>>{ { carl, USA, 43}, {Sonia, Aus, 36}, {mark, GB, 22} };

For that, you only need two dimensions
0
 
jdav3579Author Commented:
I tried the following:
String person [] [] [] = { { carl, USA, 43}, {Sonia, Aus, 36}, {mark, GB, 22} };
notice how each one has 3 elements though... the compilere just kept highlighting it as an error
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
A more flexible approach would be

Set<Person>

or

List<Person>
0
 
CEHJCommented:
String person [] [] = { { "carl", "USA", "43"}, {"Sonia", "Aus", "36"}, {"mark", "GB", "22"} };
0
 
DeanHorakCommented:
Your initializer is specifying only 2 dimensions, each with 3 elements. Notice the nesting of the curly-brackets in my example. On a three dimension array your brackets should be nested 3 deep.

BTW... to dynamically allocate a 3-dimention array use

String[][][] persion = new String[5][6][7]; // Create 3-dimensional array with 5 slots in dimension1, 6 in dimension2 and 7 in dimension3.
0
 
DeanHorakCommented:
However - as CEHJ noted, if the structure you are wanting to store in the array is

{ { "carl", "USA", "43"}, {"Sonia", "Aus", "36"}, {"mark", "GB", "22"} };

then you only need 2 dimensions...

i.e.

String[][]

I was going on the assumption that you were questioning Java's ability to handle more than 2 dimensions..... in short it certainly can...
0
 
CEHJCommented:
Unless you have a very good reason, you should use collection classes, as i mentioned above though as it will promote OO and give your more flexibility
0
 
TimYatesCommented:
>> I don't know what the limit is

255

:-)
0
 
DeanHorakCommented:
>>>> I don't know what the limit is

>>255

In the real world I have rarely seen more that three or four dimensions used.  

As an aside, an interesting note about multidimensional arrays in Java is that they are actually implemented as arrays of arrays.

Given the 2-dimensional array:
String[][] a

the element at a[n] is an array and can be referenced as so

in other words
String[] b = a[0];
would be valid.

Even stranger, there is no rule that states that each array element of the array has to have the same number of elements.

For exampe

a[0] might be an array of 10 elements
while
a[1] could be an array of 5 elements

Referencing this array as a[0][6] would cause no problem. However referencing this as a[1][6] would throw an array bounds exception!

Just a little more info to confuse :)
0
 
TimYatesCommented:
>> In the real world I have rarely seen more that three or four dimensions used.  

True, I was just telling you what the limit is in Java ;-)  I agree that I can't think of a use for a 255 dimensional array...

>> Even stranger, there is no rule that states that each array element of the array has to have the same number of elements.

That's not strange, it's called "ragged arrays" and is in C as well

I guess it depends which language you came from to Java...

Tim
0
 
DeanHorakCommented:
>>That's not strange, it's called "ragged arrays" and is in C as well
No, C implements arrays as a single contiguous vector of memory, not as an array of arrays.
You "can" implement the concept of ragged arrays in C (by creating an array of arrays!), but my point is that the default array implementation in Java is an array of arrays whereas in C the default array implementation is a single contiguous array.

>>I guess it depends which language you came from to Java...
I came from Assembler, through C/C++ to Java :)
0
 
TimYatesCommented:
>> the default array implementation is a single contiguous array.

Surely it depends how you malloc your array memory?  And with Java, how many dimensions you decide to put your array into...

For representing a 2d image in java for example it's possible using a single dimensional array of pixels.  You just have to calculate the offset into the array from the x, y and width

I'm not trying to start an argument, I'm just pointing out that Java is not as "strange" as you seem to be saying...

Heh...I'll be quiet now ;-)

Tim
0
 
CEHJCommented:
Pascal too implements them as array of array:

var Matrix: array of array of Double;
begin
SetLength(Matrix, 10, 20)
end;

Before anyone says 'hey that's Delphi!', 'array of array' was around long before Delphi ;-)
0
 
sciuriwareCommented:
The maximum # of dimensions on a 32-bit computer is ... 31
because the address generated is limited by the addressing space.
Well, [][][][][][][][][][][][][][][][][][][][][][][][][][][][][][][] is a lot.
The limit on 64-bit computers may be 255.

;JOOP!
0
 
TimYatesCommented:
>> The maximum # of dimensions on a 32-bit computer is ... 31

The maximum number of dimensions with Java is 255 (as said here: http:Q_21792691.html#16315758)
0
 
CEHJCommented:
Let's not conflate processor architecture and high-level languages folks ;-)
0
 
sciuriwareCommented:
Clever people who can create an array with 5.7896044618658097711785492504344e+76 cells, even on a 64-bit computer!

Whatever you think of it.

;JOOP!
0
 
DeanHorakCommented:
>>I'm just pointing out that Java is not as "strange" as you seem to be saying...
I didn't mean to imply that Java was "strange", I meant the use of ragged arrays was strange IMO.
0
 
TimYatesCommented:
But you can do:

    int[][] arr = new int[ 6 ][ 5 ] ;

if you want can't you (to not be ragged)?
0
 
CEHJCommented:
Raggedness is good - no enforced redundancy, and completely intuitive if seen from an 'array of array' perspective
0
 
DeanHorakCommented:
>> The maximum # of dimensions on a 32-bit computer is ... 31
TimYates is correct in that the max dimensions is 255. This is a limitation of the JVM specification. I say limitation in a strictly technical sense - in reality you will never go anywhere near that many dimensions.

As this link http://www.cafeaulait.org/books/jdr/corrections/09/index.html points out, "However if you get anywhere close to this limit you're either being deliberately perverse or doing something wrong."
0
 
sciuriwareCommented:
jdav3579, see what you have done ............................

;JOOP!
0
 
DeanHorakCommented:
Ok. I'm done. I think this dead horse has been beat enough... ;)
0
 
jdav3579Author Commented:
HI Thanks for all your comments, I found the discussion on arrays especially in other languages interesting and informative! I have decided to go another route and have four seperate arrays which I bubble sort in sync! I will have to sit down and read all the answers properly before I can decide who to award/increase and split points to.
Again thanks to all who have commented on my post.
Cheers
John
0
 
jdav3579Author Commented:
Hi, Many thanks for helping me. It is very much appreciated to everyone.
John
0
 
CEHJCommented:
:-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 8
  • 5
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now