Reputation: 827
So let's say I have an ArrayList of Strings, called list1, containing the strings, "A", "B", "C", "D", "E". Then I have another ArrayList of type storageUnit, which is a class I wrote, called list2. Please see code below for a better explanation:
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<storageUnit> list2 = new ArrayList<storageUnit>();
storageUnit newUnit = new storageUnit();
for (int i = 0; i < list1.size(); i++) {
newUnit.category = list1.get(i);
list2.add(newUnit);
}
static class storageUnit {
String category;
Hashtable<String, Integer> wordTable = new Hashtable<String, Integer>();
};
Now if I try to print all the categories of all storageUnits in list2, I get [E, E, E, E, E] instead of [A, B, C, D, E]. Could anyone explain why this is happening?
Upvotes: 2
Views: 234
Reputation: 9
Try this..
During the for loop iteration, same object(newUnit.category) is initialized for category for get the value. So each iteration it ll overrides the categories values. incase of use the below coding..
storageUnit newUnit = new storageUnit();
for (int i = 0; i < list1.size(); i++) {
newUnit.category = list1.get(i);
list2.add(newUnit);
}
put the storageUnit newUnit = new storageUnit(); with in the for loop so that it can create the new object for each iteration, and the values doesnot override.
for (int i = 0; i < list1.size(); i++) {
storageUnit newUnit = new storageUnit();
newUnit.category = list1.get(i);
list2.add(newUnit);
}
hope it help u..
Upvotes: 0
Reputation: 1986
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<storageUnit> list2 = new ArrayList<storageUnit>();
for (int i = 0; i < list1.size(); i++) {
storageUnit newUnit = new storageUnit();
newUnit.category = list1.get(i);
list2.add(newUnit);
}
static class storageUnit {
String category;
Hashtable<String, Integer> wordTable = new Hashtable<String, Integer>();
};
Couple of things. You were getting the first element of the list1 ArrayList in each iteration of the loop. Also, you were having reference issues with the newUnit object created outside of the loop. You created it once and inserted into the ArrayList a number of times. The ArrayList pointed itself at that ONE object a number of times. The last iteration of the loops set the unit to have the 'E' value so all the entries in the ArrayList (list2) were pointed at the same unit having a value of 'E'.
Upvotes: 1
Reputation: 15347
1) Start the name of classes with a capital letter. Should be StorageUnit not storageUnit.
2) You make your newUnit outside of the loop. Then add the same unit to the list 5 times. Instead, try:
for (int i = 0; i < list1.size(); i++) {
storageUnit newUnit = new storageUnit();
newUnit.category = list1.get(0);
list2.add(newUnit);
}
Upvotes: 1
Reputation: 15355
I think it should be list1.get(i)
and also you should create new unit inside the loop
Upvotes: 1
Reputation: 813
When you add newUnit to list2
, you're adding the same instance of storageUnit
5 times. This means if you change one, you'll change the rest. You need to declare a new storage unit every time you add to list2
.
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<storageUnit> list2 = new ArrayList<storageUnit>();
for (int i = 0; i < list1.size(); i++) {
storageUnit newUnit = new storageUnit();
newUnit.category = list1.get(i);
list2.add(newUnit);
}
static class storageUnit {
String category;
Hashtable<String, Integer> wordTable = new Hashtable<String, Integer>();
};
Upvotes: 2
Reputation: 25139
Change
storageUnit newUnit = new storageUnit();
for (int i = 0; i < list1.size(); i++) {
newUnit.category = list1.get(i);
list2.add(newUnit);
}
to
for (int i = 0; i < list1.size(); i++) {
storageUnit newUnit = new storageUnit();
newUnit.category = list1.get(i);
list2.add(newUnit);
}
What your code was doing was creating a single object (newUnit
) and adding that to the list multiple times. Each time, you would overwrite it's category, so when you printed the list you got the same value multiple times.
Instead, you need to create a new newUnit
object each time.
Upvotes: 6