473,473 Members | 1,584 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using arrays of objects ?

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
Feb 23 '07 #1
6 6454
Read up on collection classes and post to comp.lang.java.help
--
Regards,
Casey
Feb 23 '07 #2
TonyB <to***@kerrisway.freeserve.co.ukwrote:
>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<Toolis 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 da***@dagon.net <http://www.dagon.net/>
Feb 23 '07 #3

"TonyB" <to***@kerrisway.freeserve.co.ukwrote in message
news:E9*******************@fe04.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
here we go
http://cs.gettysburg.edu/~cpresser/j...rLoopTest.html

Joanna
Feb 25 '07 #4
TonyB wrote:
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/...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/.../Iterator.html

http://java.sun.com/docs/books/tutor...interface.html
--
Regards JJ (UWA)

Mar 1 '07 #5
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<Tooltoolbox = 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" <to***@kerrisway.freeserve.co.ukwrote in message news:E9*******************@fe04.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
|
|
Apr 14 '07 #6
Lew
TideRider wrote:
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<Tooltoolbox = new ArrayList<Tool>();
It'd likely be better to use

List<Tooltoolbox = ...

or even

Collection<Tooltoolbox = ...

For example, if a given Tool should appear only once in 'toolbox', then the
implementing class might better be a Set<Tool>.

--
Lew
Apr 14 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: don | last post by:
My question is, do C++ array of objects hold the objects or just the pointers to the objects..... I know Java arrays only hold pointers to objects, but I seem to remember that C++ arrays hold the...
5
by: Gent | last post by:
I have two questions which are very similar: Is it possible to return an object in C++. Below is part of my code for reference however I am more concerned about the concept. It seems like the...
11
by: Soeren Sonnenburg | last post by:
Hi all, Just having started with python, I feel that simple array operations '*' and '+' don't do multiplication/addition but instead extend/join an array: a= >>> b= >>> a+b
5
by: Gomaw Beoyr | last post by:
Hello Is there any explanation why Microsoft chose to implement arrays as objects allocated on the heap instead of structs allocated on the stack? For "mathematical stuff", one normally...
5
by: Alan Howard | last post by:
We're getting "ERROR (0x8007000E) Not enough storage is available to complete this operation" errors on a fairly large, busy ASP/SQL Server web site. The error is being thrown on a line calling...
25
by: Jack | last post by:
Hi, Is there a general solution for the following problem: I have an array of instances of class B. Class B is publicly derived from class A. Then I have a class named Buffer that generally...
39
by: Mark Odell | last post by:
I've always declared variables used as indexes into arrays to be of type 'size_t'. I have had it brought to my attention, recently, that size_t is used to indicate "a count of bytes" and that using...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
16
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over...
1
by: bizt | last post by:
Hi, Im currently looking to move into using JSON for AJAX instead of returning from the server a string like the following: 12345{This is a text string{true I prefer objects because I dont...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.