Hi, I need to implement an ArrayList which can hold like person records. I can only do this so far:
CODE
import java.util.*;
class ArrayListDemo {
public static void main(String args[]) {
ArrayList al = new ArrayList();
System.out.println("Initial size of al: "
+ al.size());
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
al.add(1, "A2");
System.out.println("Size of al after additions: "
+ al.size());
System.out.println("Contents of al: " + al);
al.remove("F");
al.remove(2);
System.out.println("Size of al after deletions: "
+ al.size());
System.out.println("Contents of al: " + al);
}
}
A member of this website told me to do the following, I really dont get how to get about doing that:
You do
ArrayList<Person> myList = new ArrayList<Person>();
and then you can repeatedly
newPerson = new Person("Bruce", "Wayne", 1972, "Gotham City");
myList.add(newPerson);
and you can access folks in the list by doing
int personNumber = 0;
Person retrievedPerson = myList.get(personNumber);
or even
for (Person someone : myList) {
System.out.println(someone);
}
Any help as to complete a simple program utilizing the above points mentioned by that particular stackoverflow member (ie. answer by Carl Smotricz) would be appreciated.
Thanks alot