import java.util.List;
import java.util.ArrayList;
class ListDemo{
@SuppressWarnings(value="unchecked")
public static void main(String[] args){
List list = new ArrayList();
list.add(100);
list.add(new Integer(200));
list.add("one");
list.add(new String("two"));
list.add(new Student("one",‘M‘,18));
list.add(new Student("two",‘F‘,18));
Student student = new Student();
student.setName("three");
student.setSex(‘F‘);
student.setAge(100);
list.add(student);
List list_repeat = new ArrayList();
list_repeat.add(100);
list_repeat.add(student);
Boolean bool_list = list.containsAll(list_repeat);//包含list_repeat集合
Boolean bool_obj = list.contains(student);//包含student对象
list.remove(0);//移出集合第0个元素
list.remove(student);//移出集合中的student对象的元素
list.removeAll(list);//清空集合
System.out.println(bool_list);
System.out.println(bool_obj);
System.out.println(list.size());
System.out.println("----------");
Student s = null;
for(int i=0;i<list.size();i++){
if(list.get(i) instanceof Student){
s = (Student)list.get(i);
System.out.println("name:" + s.getName() + ",sex:" + s.getSex() + ",age:" + s.getAge());
}else{
System.out.println(list.get(i));
}
}
}
}
class Student{
private String name;
private char sex;
private int age;
public Student(){
}
public Student(String name,char sex,int age){
super();
this.name = name;
this.sex = sex;
this.age = age;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setSex(char sex){
this.sex = sex;
}
public char getSex(){
return this.sex;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
}本文出自 “森林敏” 博客,请务必保留此出处http://senlinmin.blog.51cto.com/6400386/1774387
原文地址:http://senlinmin.blog.51cto.com/6400386/1774387