When I create an ArrayList or TreeMap of another Class I can only reference the last added or put element. Any pointers/explanations/help greatly received.
I am new-ish to Java (having returned to it after a bit of a longish layoff) but [probably discernable form my [code] layout] come from a heavy (mainly Visual) C++ background. What I am trying to do with the Java API, I have done many times with C++ container classes...
OK, so I want an ArrayList of a class which defines, say, 'a person'...
package arraylistcontainerprototype;
public class CPerson
{
private static String mForename;
private static String mSurname;
private static int mAge;
private static int mIndex;
// public Get/Set() methods encapsulated as normal (one example included but all follow the same code pattern)...
public String SetForename(mForename)
{
String oldForename = mForename;
this.mForename= mForename;
return oldForename;
}
public String GetForename()
{
return mForename;
}
}
Then we use that in another 'container' class, thus :- (note this code is serving to prototype the approach as my 'real application' code suffers the same issues).
package arraylistcontainerprototype;
import java.util.ArrayList;
import arraylistcontainerprototype.CPerson;
public class CContainer
{
ArrayList<CPerson> mPersonArray = new ArrayList<CPerson>();
public int SetArray()
{
mPersonArray.clear();
for(int counter = 0; counter < 5; counter++)
{
CPerson person = new CPerson();
person.SetForename("Harry" + counter);
person.SetSurname("Royston" + counter);
person.SetAge(counter);
person.SetIndex(counter);
mPersonArray.add(person);
}
// then to test the array construction...
for(int counter = 0; counter < mPersonArray.size(); counter++)
{
CPerson person = mPersonArray.get(counter);
System.out.println(person + "; counter = " + counter);
System.out.println(person.GetForename() + "; counter = " + counter);
System.out.println(person.GetSurname() + "; counter = " + counter);
System.out.println(person.GetAge() + "; counter = " + counter);
System.out.println(person.GetIndex() + "; counter = " + counter);
}
}
}
The code in main() which 'drives' the above is:-
CContainer container = new CContainer();
container.SetArray();
The output generated is:-
arraylistcontainerprototype.CPerson@15db9742; counter = 0
Harry4; counter = 0
Royston4; counter = 0
4; counter = 0
4; counter = 0
arraylistcontainerprototype.CPerson@6d06d69c; counter = 1
Harry4; counter = 1
Royston4; counter = 1
4; counter = 1
4; counter = 1
arraylistcontainerprototype.CPerson@7852e922; counter = 2
Harry4; counter = 2
Royston4; counter = 2
4; counter = 2
4; counter = 2
arraylistcontainerprototype.CPerson@4e25154f; counter = 3
Harry4; counter = 3
Royston4; counter = 3
4; counter = 3
4; counter = 3
arraylistcontainerprototype.CPerson@70dea4e; counter = 4
Harry4; counter = 4
Royston4; counter = 4
4; counter = 4
4; counter = 4
BUILD SUCCESSFUL (total time: 0 seconds)
As you can see, the references to the CPerson class instances seem OK but the calls to the 'getters' yield the 'last added' element's data.
I have dug around on the Internet and here in Stackoverflow but cannot see why this is misbehaving.
Any pointers very greatly appreciated. With many thanks in advance. (The code above should copy out easily enough if you are minded to try it. I use Netbeans and I get the same results on Windows10 and Linux (Centos/Ubuntu).