Yeh, you can have an array of Objects. And you can have one object act as a container for a collection of other objects. Here's a toy example (I normally find things easier to follow in code):
Code:
// A very simple object, but it could be a gui element,
// a complex lump of data or any other kind of object
class Example {
private String name;
// java lets you get away without setting a constructor
// google java default constructor for details
public void setName(String n) {
name = n;
}
public String getName() {
return name;
}
}
// Example Array is a 'collection' of Example objects
public class ExampleArray {
private Example[] egArray; // the container for the collection
private int size; // the size of the collection
// Here's our constructor
public ExampleArray(int s) {
size = s;
// Set up our array
egArray = new Example[size];
// Fill it with Example objects
for (int i=0;i<size;i++) {
egArray[i] = new Example();
egArray[i].setName("name: " + i);
}
}
// A little convienience method - loop
// through our collection and call the getName()
// method on each object in it
public void print() {
for (int i=0;i<size;i++) {
System.out.println(egArray[i].getName());
}
}
// Right, lets check that works
public static void main (String argv[]) {
System.out.println("Object Array Example\n===================");
// Create a new ExampleArray, with nine elements
ExampleArray e = new ExampleArray(9);
// And print it out
e.print();
}
}
And here's the output:
Code:
charlie@mogadon:~$ javac ExampleArray.java
charlie@mogadon:~$ java ExampleArray
Object Array Example
===================
name: 0
name: 1
name: 2
name: 3
name: 4
name: 5
name: 6
name: 7
name: 8
charlie@mogadon:~$
Hopefully that should make sense ...
Cheers,
Charlie
--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock:
http://charlieharvey.org.uk