Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

'Arrays of arrays' confusion

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm somewhat familiar with multidimensional arrays in BASIC varieties, but not quite understanding the arrays of arrays.

In an array of arrays of integers it seems that everything up to but excluding the last index points to a reference variable. So in the case of

int myArray [] [] [] [] = new int [5] [4] [3] [2]
(pretend I filled the arrays with ints)

There are 5*4*3*2 = 120 integers as my data, and 5*4*3 = 60 reference variables that point to arrays (any of which I can manipulate or copy as a separate entity).

A few questions:
Is the above correct.....or am I as lost as I feel?

Isn't that a lot of waste (a high proportion of reference variable data as compared to my data)?

Is it a big advantage to have the ability cut an array into pieces by using all those reference variables?

Thank you,
Paul


 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The preferred syntax is:


As for your questions, I'm not an expert on 'reference variables' so don't know if your statements are correct, but I'll answer you in a different way. Your declaration/allocation statement reserves space in memory for 5 x 4 x 3 x 2 (120) integer values. The value of each of those memory locations can be specified by an index of your variable myArray.

Here's an article that may help.

I don't think this addresses your "array of arrays" confusion, so perhaps you could restate that or add more info about your confusion.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Berry wrote:
There are 5*4*3*2 = 120 integers as my data, and 5*4*3 = 60 reference variables that point to arrays (any of which I can manipulate or copy as a separate entity).


your reference calculation 60 is ok for me. but number of integer value is not correct I think... you need to recalculate *value count <= reference count*[because of indexing] in array always!

Paul Berry wrote:
A few questions:
Isn't that a lot of waste (a high proportion of reference variable data as compared to my data)?

Is it a big advantage to have the ability cut an array into pieces by using all those reference variables?


always if you grow the arrays of arrays to arrays of arrays of arrays and so..on it makes you program to difficult to understand. instead use wrapping the references into objects or use HashMap.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're spot on. In Java, a two dimensional array is an array of arrays, and so on. Further reading: http://java.sun.com/docs/books/jls/third_edition/html/arrays.html
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul, I believe the correct number in your example is 5 + 5*4 + 5*4*3 = 85 array references. So it might be a good idea to make the largest dimension the last. If you reversed the order, it would be 2 + 2*3 + 2*3*4 = 31.

However, in practice I find that many-dimensional arrays are rare. Also, a reference is just a 32-bit or 64-bit number, so you'd probably only need to worry about it if you're using arrays with hundreds of millions of elements.
 
Marshal
Posts: 79633
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The advantage of using an array of arrays over a multi-dimensional array is that you don't have to have all the arrays the same size. You will find this sort of thing called a rectangular array:That creates a 3-element array, with references to int[] objects. Each int[] object contains 3 ints; since you haven't given them values they will all be 0s. Had you used reference types are the type of element, there would be 9 nulls. Beware of nulls.

You can also write this sort of thingThis is called a jagged array. Note numbers[1] is an empty array, but you can change that:
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think of multidimensional arrays as how we ship eggs from a farm to the store. My basic element is an egg.

An array of eggs is like an egg carton. I can define it to hold up to 12 eggs, but they don't all need to be filled.

a 2-d array would be a box of egg-cartons. my box can hold up to 20 cartons, but the cartons may not all be full. and the box does not need to contain 20 cartons.

a 3-d array might be a palate of boxes.

a 4-d would be a semi-trailer that can hold palates.

a 5-d would be my fleet of trailers

and now my analogy starts to break down...

But the point is that an 'array of arrays' is nothing more than "an array that holds THINGS...and those things might themselves be other arrays"
 
Paul Berry
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, thanks for all the replies!

It will take some time to digest it all, but it's already clearer.
 
reply
    Bookmark Topic Watch Topic
  • New Topic