How to add an object to an Enumeration

R

Rune Runnestø

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
 
R

Rune Runnestø

Rune Runnestø said:
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
 
K

Kannan

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.
 
O

Oscar kind

Rune Runnesto said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top