• 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 ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

An object to hold several arrays  RSS feed

Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
take a look at the HashMap object.
You can place an array in it.
For example

Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!