I add the details of a Person to an ArrayList twice, but when I want to print the two objects, it just repeats the final details twice (and it does not print the first).
import java.util.ArrayList;
public class Arraylist
{
Person person = new Person();
ArrayList<Person> array = new ArrayList<>();
Person personTwo;
Person personThree;
void dataEntery(String name , int age , double marks, int itc, int pf)
{
person.addDetail(name,age,marks);
person.marksDetail(itc,pf);
array.add(person);
}
void print()
{
int index =0;
while(index<array.size()) {
personTwo = array.get(index);
System.out.println(personTwo.name);
System.out.println(personTwo.age);
System.out.println(personTwo.marks);
System.out.println(personTwo.itc);
System.out.println(personTwo.pf);
index++;
}
}
}
Can anyone explain why the first isn't printing and why the last prints twice?
Arraylist; it's confusing.