Thread View: aus.computers.java
2 messages
2 total messages
Started by Steve Hewitt
Sat, 24 Jul 1999 00:00
Enumerators and Vectors...
Author: Steve Hewitt
Date: Sat, 24 Jul 1999 00:00
Date: Sat, 24 Jul 1999 00:00
38 lines
2017 bytes
2017 bytes
Can anyone please help with the following...
I have been asked to write a class called 'ItemSet' which holds a number of
Item objects (already defined).
The Items are stored within a vector and typical methods are 'addItem()',
'removeItem()', 'getItems()' etc, etc.
I have been told to use the Enumerator class but I am having a few problems.
What exactly do I need to include within the ItemSet class to make use of
the Enumerator class? I understand that it is used to iterate through
elements of an object and that I must declare two methods - public boolean
hasMoreElements() and public Object nextElement() - but how does it all fit
together?
As an example I have a method which should return all Items that are
contained within the set. I have coded this to be :
// returns a String of relevant set details
public String toString()
{
String setDetails = new String();
for (Enumeration e = items.elements() ; e.hasMoreElements() ;)
setDetails = setDetails + e.nextElement() + '\n';
return setDetails;
}
where 'items' is the Vector containing all of the Items in the set. What should the methods hasMoreElements and nextElement contain? and is the implementation of the method above correct?
Any help would be much appreciated.
Steve
steve@bert44.freeserve.co.uk
hewitts@abs-ltd.com
Re: Enumerators and Vectors...
Author: Tom Davies
Date: Sun, 25 Jul 1999 00:00
Date: Sun, 25 Jul 1999 00:00
49 lines
2494 bytes
2494 bytes
Steve,
Your code looks correct -- it would be faster if you used a
StringBuffer rather than a String, but it looks as though it works.
You don't need to write hasMoreElements() or nextElement(). The
function Vector.elements() returns an implementation of the Enumeration
interface.
Tom
Steve Hewitt wrote:
>
> Can anyone please help with the following...
>
> I have been asked to write a class called 'ItemSet' which holds a number of
> Item objects (already defined).
>
> The Items are stored within a vector and typical methods are 'addItem()',
> 'removeItem()', 'getItems()' etc, etc.
>
> I have been told to use the Enumerator class but I am having a few problems.
> What exactly do I need to include within the ItemSet class to make use of
> the Enumerator class? I understand that it is used to iterate through
> elements of an object and that I must declare two methods - public boolean
> hasMoreElements() and public Object nextElement() - but how does it all fit
> together?
>
> As an example I have a method which should return all Items that are
> contained within the set. I have coded this to be :
>
> // returns a String of relevant set details
> public String toString()
> {
> String setDetails = new String();
>
> for (Enumeration e = items.elements() ; e.hasMoreElements() ;)
> setDetails = setDetails + e.nextElement() + '\n';
>
> return setDetails;
> }
>
> where 'items' is the Vector containing all of the Items in the set. What should the methods hasMoreElements and nextElement contain? and is the implementation of the method above correct?
>
> Any help would be much appreciated.
>
> Steve
> steve@bert44.freeserve.co.uk
> hewitts@abs-ltd.com
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads