The Artima Developer Community
Sponsored Link

Java Answers Forum
ArrayList

4 replies on 1 page. Most recent reply: Jan 16, 2003 6:45 AM by Kishori Sharan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

ArrayList Posted: Dec 9, 2002 4:38 AM
Reply to this message Reply
Advertisement
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);
}
}


Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: ArrayList Posted: Jan 15, 2003 3:18 PM
Reply to this message Reply
Your program is printing correct result. Since you are testing for se == st1 it will be true for two times. Once when first "ben" is encountred and at that time you added "ben" to array list and printed [ben]. Again for st3, se == st1 will be true because you are using String literals and not String object created by new operator. and, second time it is printing [ben,ben]. If you want to see your result then please try creating String objects as:
String st1 = new String ("ben");
String st2 = new String ("alex");
String st3 = new String ( "ben" );
String st4 = new String ( "cust" );


I know that you will have some more question. Please read about String instance pooling in Java and then you should be fine.

Thanks
Kishori

Ugo Posada

Posts: 37
Nickname: binaryx
Registered: Dec, 2002

Re: ArrayList Posted: Jan 15, 2003 4:06 PM
Reply to this message Reply
Hey Kishori, I searched for the Java String instance pooling in Google and nothing remotely close appeared. Do you have a good resource to read about it 'cause it seems fairly interesting. Where did you learn that?

Cheers.

Ugo Posada Zabala

Ugo Posada

Posts: 37
Nickname: binaryx
Registered: Dec, 2002

Re: ArrayList Posted: Jan 15, 2003 4:10 PM
Reply to this message Reply
Does it have anything to do with the fact that he is comparing references instead of using the method equals? Maybe I can see the point there.

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: ArrayList Posted: Jan 16, 2003 6:45 AM
Reply to this message Reply
Hi Ugo
Here is the link that explains String pooling. I posted this message more than two years ago.
http://www.artima.com/legacy/answers/Nov2000/messages/251.html
Thanks
KIshori

Flat View: This topic has 4 replies on 1 page
Topic: question about java arguments Previous Topic   Next Topic Topic: The game of Red Dog

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use