Results 1 to 8 of 8
Thread: Order whilst Enumerating
- 06-26-2011, 01:00 AM #1
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.>> What can be asserted without proof can be dismissed without proof. <<
- 06-26-2011, 01:48 AM #2
Can you make a simple program that executes and shows the problem? And post it here.for some strange reason, that ain't happening for me !
- 06-26-2011, 02:17 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
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.I have checked the API, but I saw nothing that would answer my above query
- 06-26-2011, 02:36 AM #4
Must be the OP is thinking of Iterator.
- 06-26-2011, 11:33 AM #5
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
.gif)
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:
How can I test the above program in Eclipse ?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 */
Thanks>> What can be asserted without proof can be dismissed without proof. <<
- 06-26-2011, 01:56 PM #6
Your code needs to show what "ain't happening". Does what you posted show your problem?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 !
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.
- 06-26-2011, 02:15 PM #7
You're right it doesn't, I'm sorry
.gif)
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.>> What can be asserted without proof can be dismissed without proof. <<
- 06-26-2011, 03:15 PM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,399
- Blog Entries
- 7
- Rep Power
- 17
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.When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
order of sequence
By dannyy in forum New To JavaReplies: 23Last Post: 04-14-2011, 07:29 PM -
Inheriting an inner class whilst using the variables from a subclass
By loki20884 in forum New To JavaReplies: 3Last Post: 03-29-2011, 08:29 AM -
Order of EXECUTION followed by JVM!
By _ShivamKapoOr_ in forum New To JavaReplies: 5Last Post: 09-24-2010, 09:18 PM -
LinkedHashMap insertion whilst iterating
By Paul Richards in forum Advanced JavaReplies: 7Last Post: 02-13-2009, 01:24 AM -
Descending order
By santanu in forum New To JavaReplies: 1Last Post: 11-04-2008, 04:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks