AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Using arrays of objects ?

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> comp.lang.java
View previous topic :: View next topic  
Author Message
TonyB
Guest





PostPosted: Sat Feb 24, 2007 3:38 am    Post subject: Using arrays of objects ? Reply with quote



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
Back to top
Casey Hawthorne
Guest





PostPosted: Sat Feb 24, 2007 3:48 am    Post subject: Re: Using arrays of objects ? Reply with quote



Read up on collection classes and post to comp.lang.java.help
--
Regards,
Casey
Back to top
Mark Rafn
Guest





PostPosted: Sat Feb 24, 2007 4:30 am    Post subject: Re: Using arrays of objects ? Reply with quote



TonyB <tonyb (AT) kerrisway (DOT) freeserve.co.uk> wrote:
Quote:
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.

Quote:
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 (AT) dagon (DOT) net <http://www.dagon.net/>
Back to top
Joanna
Guest





PostPosted: Sun Feb 25, 2007 8:10 am    Post subject: Re: Using arrays of objects ? Reply with quote

"TonyB" <tonyb (AT) kerrisway (DOT) freeserve.co.uk> wrote in message
news:E9JDh.87996$tQ1.64679 (AT) fe04 (DOT) news.easynews.com...
Quote:
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

here we go
http://cs.gettysburg.edu/~cpresser/java/ForLoopTest.html

Joanna
Back to top
j1mb0jay
Guest





PostPosted: Thu Mar 01, 2007 11:19 pm    Post subject: Re: Using arrays of objects ? Reply with quote

TonyB wrote:
Quote:
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

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
--
Regards JJ (UWA)
Back to top
TideRider
Guest





PostPosted: Sat Apr 14, 2007 7:50 pm    Post subject: Re: Using arrays of objects ? Reply with quote

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
}


--
TideRider


"TonyB" <tonyb (AT) kerrisway (DOT) freeserve.co.uk> wrote in message news:E9JDh.87996$tQ1.64679 (AT) fe04 (DOT) news.easynews.com...
| 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
|
|
Back to top
Lew
Guest





PostPosted: Sat Apr 14, 2007 8:12 pm    Post subject: Re: Using arrays of objects ? Reply with quote

TideRider wrote:
Quote:
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
Quote:
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>.

--
Lew
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> comp.lang.java All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.