本人的理解:
有许多时候使用ArrayList进行添加删除数据,里面的值是多样的,虽然可以通过HashMap解决问题,但是自定义的Bean更容易理解.
如:
public class Test{
//实例化
List<PeopleInfo> lists;
class PeopleInfo{
//身份证
long id;
//姓名
String name;
//年龄
int age;
public PeopleInfo(long id,String name,int age){
this.id = id;
this.name = name;
this.age = age;
}
public void setId(long id){
this.id = id;
}
public long getId(){
return id;
}
public void setName(String name){
this.name = name;
}
public String getName(String name){
return name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
}
//初始化数据
void initData(){
lists = new ArrayList<PeopleInfo>()
lists.add(new PeopleInfo(1,"wo",20));
lists.add(new PeopleInfo(2,"woshi",20));
lists.add(new PeopleInfo(3,"woshiku",20));
}
//打印数据
void void printData(){
//取数据的二种方法
//方法1
/*for(PeopleInfo list:lists){
System.out.println("身体证"+list.getId()+"姓名"+list.getName()+"年龄"+list.getAge());
}*/
//方法二
for(int i=0;i<lists.size();i++){
PeopleInfo list = lists.get(i);
System.out.println("身体证"+list.getId()+"姓名"+list.getName()+"年龄"+list.getAge());
}
}
public static void main(String[] args){
Test test = new Test();
test.initData();
test.printData();
}
}
楼主可以运行感受一下,删除用list.remove(position)就行,看看是不是逻辑更加清晰了 |