我需要实现一个可以保存个人记录的ArrayList。到目前为止我只能做到这一点:
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);
}
}
这个网站的一位成员告诉我要做以下事情,我真的不知道如何做到这一点:
你做
ArrayList<Person> myList = new ArrayList<Person>();
然后你可以重复
newPerson = new Person("Bruce", "Wayne", 1972, "Gotham City");
myList.add(newPerson);
您可以通过
访问列表中的人员int personNumber = 0;
Person retrievedPerson = myList.get(personNumber);
甚至
for (Person someone : myList) {
System.out.println(someone);
}
使用上述特定stackoverflow member(即Carl Smotricz的回答)提到的完整简单程序的任何帮助都将不胜感激。
非常感谢
答案 0 :(得分:-2)
完成此
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package sl4jdemo;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Administrator
*/
public class arrayTest {
public static class Person{
String name;
String SSL;
String Street;
Integer age;
public String getSSL() {
return SSL;
}
public void setSSL(String SSL) {
this.SSL = SSL;
}
public String getStreet() {
return Street;
}
public void setStreet(String Street) {
this.Street = Street;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person(String name, String SSL, String Street, Integer age) {
this.name = name;
this.SSL = SSL;
this.Street = Street;
this.age = age;
}
}
public static void main(String[] args) {
List<Person> persons = new ArrayList<arrayTest.Person>();
persons.add(new Person("Bob", "FKJHHS87546", "WALLSTREET", 21));
persons.add(new Person("Tom", "SEFSDF875463", "WALLSTREET", 25));
for (Person person : persons) {
System.out.println("Name : "+person.getName());
System.out.println("SSL : "+person.getSSL());
System.out.println("Street : "+person.getStreet());
System.out.println("Age : "+person.getAge());
}
}
}