Ahh..I get it. I thought when I did wordPairList.add, it added it by value,
list and copied the values from tempWordPair to the new entry in the list.
That explains a lot! Thanks - this was a roadblock. This list will hold data
what I posted here. This code was trimmed down to illustrate the issue I was
seeing. The actual code is much bigger, and I saw this behavior in several
places, and for the same reason.
Hi,
The reason for the following output is
[0] - ccc
[1] - ccc
[2] - ccc
When the tempWordPair object is created, WordPair tempWordPair = new
WordPair();, it will hold the reference(nothin but the memory location)
to the object, say tempWordPair holds the memory location as 12121. Now
in that location, firstWord variable is set to "aaa". Now u r adding
the reference to the Arraylist, nothing but 12121.
Again u have changed the firstWord to "bbb" and added to the arraylist
nothing but 12121.
Again u have changed the firstWord to "ccc" and added to the arraylist
nothing but 12121.
Finally, when u iterate the Arraylist, u will retrieving the same
object at the same reference,...so u always get the value "ccc"
To overcome this...try to create new object each time, before u set the
firstWord...
import java.util.*;
public class Assignment1
{
static class WordPair
{
String firstWord;
}
public static void main(String[] args)
{
WordPair tempWordPair = new WordPair();
ArrayList<WordPair> wordPairList = new ArrayList<WordPair>();
tempWordPair.firstWord = "aaa";
wordPairList.add( tempWordPair );
tempWordPair = new WordPair();
tempWordPair.firstWord = "bbb";
wordPairList.add( tempWordPair );
tempWordPair = new WordPair();
tempWordPair.firstWord = "ccc";
wordPairList.add( tempWordPair );
System.out.println("[0] - " + wordPairList.get(0).firstWord);
System.out.println("[1] - " + wordPairList.get(1).firstWord);
System.out.println("[2] - " + wordPairList.get(2).firstWord);
}
}
On Jan 18, 9:18 am, "Ook" <Ook Don't send me any freakin' spam at
zootal dot com delete the Don't send me any freakin' spam> wrote:
I think this is a bit tougher then the last one I posted (which was quite
simple). I think I'm missing something fundamental. This code runs, but
when
I run it and it prints out the contents of the list it shows that all
items
in the list have the same value as the last item I added. I expect to see
in
the console:
[0] - aaa
[1] - bbb
[2] - ccc
But instead I see
[0] - ccc
[1] - ccc
[2] - ccc
------------------------------------------------------
import java.util.*;
public class Assignment1
{
public static class WordPair
{
String firstWord;
}
public static void main(String[] args)
{
WordPair tempWordPair = new WordPair();
ArrayList<WordPair> wordPairList = new ArrayList<WordPair>();
tempWordPair.firstWord = "aaa";
wordPairList.add( tempWordPair );
tempWordPair.firstWord = "bbb";
wordPairList.add( tempWordPair );
tempWordPair.firstWord = "ccc";
wordPairList.add( tempWordPair );
System.out.println("[0] - " + wordPairList.get(0).firstWord);
System.out.println("[1] - " + wordPairList.get(1).firstWord);
System.out.println("[2] - " + wordPairList.get(2).firstWord);
}
}