Results 1 to 8 of 8
  1. #1
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default Order whilst Enumerating

    Hello Awesome Java People.

    I have a question about the way the Enumeration abstract class works. I think I can explain this best with an image. Take the following ArrayList<Integer>:



    When I am iterating through the above ArrayList with the Enumeration, I use the two methods:

    hasMoreElements() and nextElement()

    My question is this. Does the Enumeration occur in order ? i.e. would the first element to be grabbed by nextElement be the number 30 according to the ArrayList diagram above ?

    If it is in Order, then for some strange reason, that ain't happening for me !

    Any help would be great. PS. I have checked the API, but I saw nothing that would answer my above query.

    Thank You.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default

    for some strange reason, that ain't happening for me !
    Can you make a simple program that executes and shows the problem? And post it here.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I have checked the API, but I saw nothing that would answer my above query
    In fact the API docs for ArrayList don't provide any way of obtaining an Enumeration instance (that I can see). So some code to show what you are doing (SSCCE) would be a good idea.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default

    Must be the OP is thinking of Iterator.

  5. #5
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Can you make a simple program that executes and shows the problem? And post it here.
    Hehe so each time I have a simple query such as above, I need to create a little program to test it ! The problem with that is, in eclipse I still don't know how to make programs run

    As far as I know, one needs to create a Static Main Method, but what do I write in that method, I have no idea. Take the following program:

    Java Code:
    /*
      Iterate through keys of Java Hashtable example
      This Java Example shows how to iterate through the keys contained in the
      Hashtable object.
    */
     
    import java.util.Hashtable;
    import java.util.Enumeration;
     
    public class IterateThroughKeysOfHashtableExample {
     
      public static void main(String[] args) {
     
        //create Hashtable object
        Hashtable ht = new Hashtable();
     
        //add key value pairs to Hashtable
        ht.put("1","One");
        ht.put("2","Two");
        ht.put("3","Three");
     
        /*
          get Enumeration of keys contained in Hashtable using
          Enumeration keys() method of Hashtable class
        */
        Enumeration e = ht.keys();
     
        //iterate through Hashtable keys Enumeration
        while(e.hasMoreElements())
          System.out.println(e.nextElement());
      }
    }
     
    /*
    Output would be
    3
    2
    1
    */
    How can I test the above program in Eclipse ?

    Thanks

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,572
    Rep Power
    25

    Default

    Does the Enumeration occur in order ? i.e. would the first element to be grabbed by nextElement be the number 30 according to the ArrayList diagram above ?

    If it is in Order, then for some strange reason, that ain't happening for me !
    Your code needs to show what "ain't happening". Does what you posted show your problem?

    You've used an HashTable in your sample code, not an ArrayList that your question was asking about.

    Can you copy the program's output and paste it here and add comments to the that output describing what is wrong with it?

    It appears that the keys are ordered high to low.

  7. #7
    Ciwan is offline Banned
    Join Date
    Dec 2008
    Location
    Earth
    Posts
    87
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Your code needs to show what "ain't happening". Does what you posted show your problem?
    You're right it doesn't, I'm sorry

    Quote Originally Posted by Norm View Post
    You've used an HashTable in your sample code, not an ArrayList that your question was asking about.
    I assumed the way an Enumeration would behave would be the same for either a Hashtable or an ArrayList !

    I have looked into it further, and it turns out the Enumeration does react in order. Which means the issue is from my Hashtable, and its keys.

    I am going to have to create another thread to tackle the new problem (not that of Hashtable and keys). I've been at it all morning to no avail.

    Thanks Norm.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,658
    Blog Entries
    7
    Rep Power
    21

    Default

    An Iterator (or Enumeration for that matter) iterates over the elements of a collection in the order the elements are internally stored in that collection. For ArrayLists an Iterator runs front to back from the element stored at position 0 to the last element (with the largest index value). For HashTables (or HashMaps) Iterators run over the internal tables that store the elements according to their hash valuie (i.e. no particular order). A sorted map/set makes the Iterator run over the elements in their natural order. Iterators or Enumerations can't change that order at all.

    kind regards,

    Jos

    ps. HashTables implement Enumeration in terms of an Iterator internally.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. order of sequence
    By dannyy in forum New To Java
    Replies: 23
    Last Post: 04-14-2011, 07:29 PM
  2. Replies: 3
    Last Post: 03-29-2011, 08:29 AM
  3. Order of EXECUTION followed by JVM!
    By _ShivamKapoOr_ in forum New To Java
    Replies: 5
    Last Post: 09-24-2010, 09:18 PM
  4. LinkedHashMap insertion whilst iterating
    By Paul Richards in forum Advanced Java
    Replies: 7
    Last Post: 02-13-2009, 01:24 AM
  5. Descending order
    By santanu in forum New To Java
    Replies: 1
    Last Post: 11-04-2008, 04:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •