How to add an object to an Enumeration

Discussion in 'Java' started by Rune Runnestø, Apr 27, 2005.

  1. 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
    Rune Runnestø, Apr 27, 2005
    #1
    1. Advertising

  2. "Rune Runnestø" <> skrev i melding
    news:XZSbe.8017$...
    > 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
    Rune Runnestø, Apr 28, 2005
    #2
    1. Advertising

  3. Rune Runnestø

    Kannan Guest

    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.
    Kannan, Apr 28, 2005
    #3
  4. Rune Runnestø

    Oscar kind Guest

    Rune Runnesto" <> 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
    Oscar kind, Apr 28, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timothy Parez
    Replies:
    3
    Views:
    3,768
    WhatIThink
    Jun 29, 2010
  2. Jan Kindt
    Replies:
    5
    Views:
    2,039
    Allan Herriman
    Sep 12, 2003
  3. Replies:
    15
    Views:
    1,316
    Jerry Coffin
    Feb 1, 2005
  4. puvit82
    Replies:
    4
    Views:
    761
    puvit82
    Feb 1, 2008
  5. MyndPhlyp
    Replies:
    4
    Views:
    135
    MyndPhlyp
    Oct 2, 2005
Loading...

Share This Page