I'm new to java, and would like some advice on how to keep track of groups
of objects where the number of objects is not predefined.
For example say I have a toolbox class which contains an arbitrary number of
tool objects. I want to have a method to add a tool to the toolbox object,
and other methods to list the current contents of the toolbox for example.
I could keep a reference to each tool object as added in an array of type
tool but an array is declared with a fixed length, so short of defining the
array to have a some arbitrary large size I can't see how to deal with this
list of objects. I don't want to use a database at this time.
Is there a better way to do this ?
Tony
Casey Hawthorne - 23 Feb 2007 21:48 GMT
Read up on collection classes and post to comp.lang.java.help
--
Regards,
Casey
Mark Rafn - 23 Feb 2007 22:30 GMT
>I'm new to java, and would like some advice on how to keep track of groups
>of objects where the number of objects is not predefined.
java.util.Collection and friends make this pretty easy.
>I could keep a reference to each tool object as added in an array of type
>tool but an array is declared with a fixed length, so short of defining the
>array to have a some arbitrary large size I can't see how to deal with this
>list of objects. I don't want to use a database at this time.
ArrayList<Tool> is the most common way to do this. Depending on your needs,
you may prefer a HashSet, TreeSet, LinkedList, or some other Collection that
fits your performance and usage requirements.
--
Mark Rafn dagon@dagon.net <http://www.dagon.net/>
Joanna - 25 Feb 2007 05:22 GMT
> I'm new to java, and would like some advice on how to keep track of groups
> of objects where the number of objects is not predefined.
[quoted text clipped - 7 lines]
> Is there a better way to do this ?
> Tony
here we go
http://cs.gettysburg.edu/~cpresser/java/ForLoopTest.html
Joanna
j1mb0jay - 01 Mar 2007 17:19 GMT
> I'm new to java, and would like some advice on how to keep track of
> groups of objects where the number of objects is not predefined.
[quoted text clipped - 7 lines]
> to use a database at this time. Is there a better way to do this ?
> Tony
ArrayLists would seem to the answer for what you are looking for.
http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html
There is a Iterator class for going through each of the objects in a array,
try not to store objects in an array, try to type them first (looking into
Interfaces).
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Iterator.html
http://java.sun.com/docs/books/tutorial/java/concepts/interface.html

Signature
Regards JJ (UWA)
TideRider - 14 Apr 2007 15:50 GMT
I would probably use an ArrayList (from java.util). I would also define either an
interface or base class for Tool, from which all of the tools are derived, and
(using Java 5 or higher) parameterize the ArrayList:
ArrayList<Tool> toolbox = new ArrayList<Tool>();
To operate on Tool in the toolbox (again, with Java 5 or higher), you can use:
for (Tool tool : toolbox)
{
//tool variable contains the current tool
}

Signature
TideRider
| I'm new to java, and would like some advice on how to keep track of groups
| of objects where the number of objects is not predefined.
[quoted text clipped - 7 lines]
| Is there a better way to do this ?
| Tony
Lew - 14 Apr 2007 16:12 GMT
> I would probably use ...
Just so you know, many newsreaders, mine included, clip material that follows
your sig in the reply, and display it "dimmed" or gray in the initial reading.
This happened with your replies, which were top-posted.
Top-posting also disrupts the flow of the message and makes it harder to read.
Please don't top-post.
As to your suggestion to use
> ArrayList<Tool> toolbox = new ArrayList<Tool>();
It'd likely be better to use
List<Tool> toolbox = ...
or even
Collection<Tool> toolbox = ...
For example, if a given Tool should appear only once in 'toolbox', then the
implementing class might better be a Set<Tool>.

Signature
Lew