Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to add an object to an Enumeration

Reply
Thread Tools

How to add an object to an Enumeration

 
 
Rune RunnestÝ
Guest
Posts: n/a
 
      04-27-2005
I'm trying to make a method in a jsp-file like this one:
The method has an Enumeration object and a String object as arguments. Don't
bother about the validation, it is taken care of by another method. Just
assume that it turned out OK.

<%!
public void addThisNumber(Enumeration list, String thisNumber) throws
Exception{
int oneNumber = validateThisNumber(list, thisNumber);
//the validation turned out OK, how do I add the number to the list
? Is an Iterator the solution ? Eventually, how ?
.....
.....
//the sentence below would have been right if it was an ArrayList,
but it does'n work for an Enumberation.
// alleTalla.add(new Integer(eitTall));
}
%>


Regards
Rune


 
Reply With Quote
 
 
 
 
Rune RunnestÝ
Guest
Posts: n/a
 
      04-28-2005

"Rune RunnestÝ" <(E-Mail Removed)> skrev i melding
news:XZSbe.8017$(E-Mail Removed)...
> I'm trying to make a method in a jsp-file like this one:
> The method has an Enumeration object and a String object as arguments.

Don't
> bother about the validation, it is taken care of by another method. Just
> assume that it turned out OK.
>
> <%!
> public void addThisNumber(Enumeration list, String thisNumber) throws
> Exception{
> int oneNumber = validateThisNumber(list, thisNumber);
> //the validation turned out OK, how do I add the number to the

list
> ? Is an Iterator the solution ? Eventually, how ?
> .....
> .....
> //the sentence below would have been right if it was an

ArrayList,
> but it does'n work for an Enumberation.


Correction, the sentence below should be
list.add(new Integer(oneNumber));

> // alleTalla.add(new Integer(eitTall));
> }
> %>


Rune


 
Reply With Quote
 
 
 
 
Kannan
Guest
Posts: n/a
 
      04-28-2005
Rune,
An Enumeration is a general interface which can give you an enumeration
of elements in collection. (Same is the case with Iterator).

The reason there is no method like
void addElement(Object) in Enumeration is simply because the
enumeration has REALLY NO IDEA of the underlying the collection.

A collection in general can be a List, Set, HashSet (or anything else
like a Map) and adding an element mean will mean different things in
these different classes.

I think this must answer your question.

YOU should change the method signature to take up the underlying
collection. I dont think there is any other way out.

 
Reply With Quote
 
Oscar kind
Guest
Posts: n/a
 
      04-28-2005
Rune Runnesto" <(E-Mail Removed)> wrote:
> I'm trying to make a method in a jsp-file like this one:
> The method has an Enumeration object and a String object as arguments. Don't
> bother about the validation, it is taken care of by another method. Just
> assume that it turned out OK.
>
> <%!
> public void addThisNumber(Enumeration list, String thisNumber) throws
> Exception{
> int oneNumber = validateThisNumber(list, thisNumber);
> //the validation turned out OK, how do I add the number to the list
> ? Is an Iterator the solution ? Eventually, how ?
> .....
> .....
> //the sentence below would have been right if it was an ArrayList,
> but it does'n work for an Enumberation.
> // alleTalla.add(new Integer(eitTall));
> }
> %>


As already noted, an Enumeration or an Iterator has no concept of the
ounderlying collection being enumerated. Therefore, it cannot add an
element.

It is possible however, to use a ListIterator is the underlying collection
is a List. In that interface, an add method is defined -- although it is
an optional operation; you'll have to test if your collection implements
it.


--
Oscar Kind http://home.hccnet.nl/okind/
Software Developer for contact information, see website

PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Filling a listbox with the values in a enumeration Timothy Parez ASP .Net 3 06-29-2010 05:59 AM
Re-using a simple type definition; with enumeration constraint andwithout enumeration constraint puvit82 XML 4 02-01-2008 03:46 PM
Server-side enumeration of form object names and values? MyndPhlyp ASP General 4 10-02-2005 07:47 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM



Advertisments