File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

ArrayList

ben riches
Ranch Hand

Joined: Nov 08, 2002
Posts: 126
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

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.


Jon
 
wood burning stoves
 
subject: ArrayList