• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList

 
ben riches
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why does my arraylist print out [ben][ben, ben] when it should print out
ben
ben
could anyone help me please?
my code is below
thanks
loftty
ArrayList arr = new ArrayList();
Hashtable hash = new Hashtable();
String st1 = "ben";
String st2 = "alex";
String st3 = "ben";
String st4 = "cust";
hash.put("a", st1);
hash.put("b", st2);
hash.put("c", st3);
hash.put("d", st4);
Enumeration e = hash.elements();
while (e.hasMoreElements()) {
String se = (String)e.nextElement();


if (se == st1){

arr.add(se);
System.out.println(arr);
}
}
 
Jon Strayer
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because that's what it was supposed to do. You are printing out the entire list every time you add something to it. It sounds like you want to use a Set instead of a List.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic