• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Enumerators and Iterators

 
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I go about returning the elements of enumerators or iterators in a correct order. My code returns the 2nd element first, then the first element, then the 3rd and 4th elements. I do not understand. Please take a look at this code. Thanks!!!

import java.awt.*;
import javax.swing.*;
import java.util.Hashtable;
import java.util.Iterator;
public class Test5000
{
Hashtable hash = new Hashtable();

public static void main(String [] args)
{
new Test5000();
}

public Test5000()
{
String str1 = "One item";
String str2 = "matches this item1";
String str3 = "Second item";
String str4 = "matches this item2";
String str5 = "Third item";
String str6 = "matches this item3";
String str7 = "Fourth item";
String str8 = "matches this item4";

hash.put(str2,str2);
hash.put(str3,str4);
hash.put(str5,str6);
hash.put(str7,str8);

Iterator keys = (Iterator) hash.keys();
Iterator elements = (Iterator) hash.elements();

while (keys.hasNext())
{
String s1 = (String) keys.next();
String s2 = (String) elements.next();
System.out.println(s1 + " " + s2);
}
}
}

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds like the use of a hashtable doesn't fit your needs. You could either use an ordered collection (e.g. Vector) or if you still need keyed lookups, use a collection type that best fits your primary data lookup needs, then morph that collection into another form where it is used secondarily.
 
Barry Andrews
Ranch Hand
Posts: 529
C++ Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!!! You have been a great help!
 
Without subsidies, chem-ag food costs four times more than organic. Or this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic