File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes An object to hold several arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "An object to hold several arrays" Watch "An object to hold several arrays" New topic

An object to hold several arrays

Jerry McClain

Joined: Jul 15, 2002
Posts: 5
I am completely lost on this issue, so as much information I can get would be helpful.
Is there an object that will hold several arrays? Then when I call that object, I would like to reference specific element from a specific array held within this object. Does anything like this exist ?
I am just reading about Collections, but I have not found anything that could do this.
Thanks for any help.
Marcel de Jong
Ranch Hand

Joined: May 27, 2002
Posts: 54
take a look at the HashMap object.
You can place an array in it.
For example

Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
An array is an object. Arrays can be constructed of primitives or objects - each component of the array must be of the same type. A Collection can hold objects of type Object. In Java, all objects descend from Object. Object is the grandparent of all objects in Java.
So, you can have an array of arrays and you can have a Collection of objects that are arrays (because arrays are objects).
You probably already know how to create and use an array of primitive data types (things like int or char). Have you created and used an array of Strings? Strings are objects. You could similarly create and use an array of arrays. Arrays are objects.
Consider briefly an array of Strings:
String day = "Third" ;
String month = "May" ;
String year = "2003" ;
String[] strings = { day , month , year };
In order to use a component of this array (without just using the original reference), that array element must be dereferenced. A reference to that component could be maintained for future convenience and effeciency.
Let's say we want to inspect and use the value that represents the month and we only have access to the strings array (we don't have access to the original String references that were used to create the array of Strings). We simply dereference the array and get the value:
String month = strings[ 1 ];
It's pretty much the exact same thing for handling arrays of arrays (which are just arrays of objects).
Consider these arrays of ints:
int[] nums1 = { 1 , 2 , 3 };
int[] nums2 = { 4 , 5 , 6 , 7 };
int[] nums3 = { 8 , 9 };
We have a couple of different types of arrays that we could create to contain the three arrays above. This first strategy you probably won't use - I'm introducing it to hammer away at the notion that arrays are objects. Knowing that arrays are objects, we could create an array of objects. The components of this array would be regular old objects:
Object[] objects = { nums1 , nums2 , nums3 };
To access the third component of the nums2 array (which is the second component of the objects array), we could do something like this:
int[] numbers = (int[])objects[ 1 ];
int six = numbers[ 2 ];
In this case, the numbers reference would reference the same array that the num2 array referenced. Note that an explicit cast to an int array was necessary after we got the Object from the array of objects. Such casts are often required when dealing with Collections that can only hold objects of type Object.
There is a nicer way to deal with arrays of arrays in Java (rather than deal with arrays of objects that are arrays as demonstrated above) - that is to deal with arrays of arrays directly (multidimensional arrays). A simple example of a two-dimensional int array is:
int[][] numbers = { { 1 , 2 } , { 3 , 4 , 5 } , { 6 } };
Here we have a two-dimensional int array with three components in the first dimension (three one-dimensional int arrays). The first component is a one-dimensional int array with two components, the second component is a one-dimensional int array with three components, and the third component is a one-dimensional int array with one component.
To use the third component of the second component array (so the number 5), we dereference the two-dimensional array like this:
int five = numbers[ 1 ][ 2 ];
That was a bit long-winded, but I thought that perhaps a long response would cure my insomnia tonight - and it has. I hope that I've driven home the idea that an array is an object that can hold multiple arrays that in turn are each objects that can hold another layer of arrays, etc.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
I agree. Here's the link:
subject: An object to hold several arrays
It's not a secret anymore!