public static void main(String[] args) {
//实例化对象
LinkedList linkedList=new LinkedList();
//1添加元素
Student student=new Student("刘德华",20);
Student student1=new Student("郭富城",30);
Student student2=new Student("张三",33);
linkedList.add(student);
linkedList.add(student1);
linkedList.add(student2);
System.out.println("元素个数:"+linkedList.size());
System.out.println(linkedList.toString());
//2删除元素
linkedList.remove(new Student("张三",33));
System.out.println("删除之后的元素个数:"+linkedList.size());
//linkedList.clear();//清空
//遍历
//3.1for遍历
System.out.println("--------------for遍历-----------");
for (int i = 0; i <linkedList.size() ; i++) {
System.out.println(linkedList.get(i));
}
//3.2增强for
System.out.println("--------------增强for遍历-----------");
for (Object object:linkedList) {
Student s=(Student) object;
System.out.println(s.toString());
}
//3.3使用迭代器
System.out.println("--------------迭代器遍历-----------");
Iterator it =linkedList.iterator();
while (it.hasNext()){
Student s=(Student) it.next();
System.out.println(s.toString());
}
//3.4使用列表迭代器
System.out.println("--------------列表迭代器遍历-----------");
ListIterator lit=linkedList.listIterator();
while (lit.hasNext()){
Student s=(Student) lit.next();
System.out.println(s.toString());
}
//4判断
System.out.println(linkedList.contains("郭富城"));
System.out.println(linkedList.isEmpty());
//获取
System.out.println(linkedList.indexOf(student1));
}
标签:LinkedList,linkedList,System,Student,new,out
From: https://www.cnblogs.com/anonymity12/p/18402188