Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Java in General and the fly likes ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ArrayList" Watch "ArrayList" New topic


ben riches
Ranch Hand

Joined: Nov 08, 2002
Posts: 126
why does my arraylist print out [ben][ben, ben] when it should print out
could anyone help me please?
my code is below
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){

Jon Strayer
Ranch Hand

Joined: Dec 04, 2002
Posts: 133
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.

I agree. Here's the link:
subject: ArrayList
It's not a secret anymore!