10 Replies Latest reply: Apr 22, 2008 8:09 AM by 800308

# individual dimensions of a md array

Hello
How/is it possible to get the individual dimensions of a multi-dimensional array? I want to setup for loops and cant figure how to do it for each of the dimensions. tia.
• ###### 1. Re: individual dimensions of a md array
``````int[][] array = new int[5][4];
int dim1 = array.length; //5
int dim2 = array[0].length; //4``````
• ###### 2. Re: individual dimensions of a md array
Hi newark
Thanks. Which leads me to another questions. If I wanted a multi-dimensional array of objects, is the best/only way to do it as one ArrayList with additional Arraylists created for each row?
• ###### 3. Re: individual dimensions of a md array
Why would you have to use ArrayList vs. normal arrays? What's wrong with this:
``````String [][][] stringArray = new String[5][5][5];
for (int i = 0; i < 5; i++)
for (int j = 0; j < 5; j++)
for (int k = 0; k < 5; k++)
stringArray[i][j][k] = new String();``````
• ###### 4. Re: individual dimensions of a md array
You can create a two dimensional array of Objects.

You can also create a two dimensional ArrayList, as you say, with an ArrayList of ArrayLists... or a Vector of Vector's... or indeed a Collection of any other type of Collection... mix and match as you see fit.

One thing to remember with big collections is that arrays are a LOT smaller than Collections. Like 200,000 bytes in an array is about 200k... the same data in an ArrayList is 600k... ouch!
• ###### 5. Re: individual dimensions of a md array
Hi newark
That give a homogeneous array, I have object of different types and I want to keep them in their native types. So is the ArrayList(ArrayList)) the only and proper way?
• ###### 6. Re: individual dimensions of a md array
You want a single array which holds different types of objects? Like, the same array will hold both Trinkets and Doodads? What's your reason for this?
• ###### 7. Re: individual dimensions of a md array
Hi newark
I am parsing a csv file using ostermiller's utilities (vnice) and, of course, everything comes over as Strings. In the parsing I want to convert what I know to be objects, ints etc, into full objects and keep the true strings as Strings. So any given column in a row will be either a String or a int, therefore a hetrogenous array. So my thinking is I need a data structure that will allow me to store objects in their native format as a n-dimensional array. Albeit cumbersome an ArrayList(ArrayList)) seems natural. Ideas? tia.
• ###### 8. Re: individual dimensions of a md array
Are the rows of your file related in any way? For example, if you had a csv file for a weight loss program:
John, 240, 210
Sally 170, 130
Where each row has a name (String), a current weight (int), and a goal weight (int). Now, if this is in any way similar to your csv file, where rows contain related data, you would create objects based on the row data.
``````class WeightLossCustomer {
String name;
int currentWeight;
int goalWeight;

public WeightLossCustomer(String n, int c, int g) {
name = n;
currentWeight = c;
goalWeight = g;
}
}``````
Then you'd create a 1D array of WeightLossCustomer's instead of some multidimensional array to store multiple data types.

Sorry if this is in no way relevant to your application.
• ###### 9. Re: individual dimensions of a md array
Hi newark
You are one level higher than me. I wanted to keep the parsing object clean and pass off a hetro array to the object that knows how to format it. You are right each record is an object and I could create the object at the time of parsing and just pass off a simple ArrayList(). So is my approach correct/best for the lower level I need to implement?
• ###### 10. Re: individual dimensions of a md array
Yes, I think your approach should work. Create an ArrayList of ArrayLists of Objects and it should be fine.

But remember, ArrayLists cannot hold primitive types like int. If you add a primitive to an ArrayList, it will autobox it into a primitive wrapper class like Integer. You'll need to know that when you try to get your data back out.

Edited by: newark on Apr 22, 2008 11:01 AM