LinkedListUse
LinkedListUse类:
package com.tiedandan.集合.LinkedList使用;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import java.util.LinkedList;
public class LinkedlistUse {
public static void main(String[] args) {
LinkedList link1 = new LinkedList();
Student stu1 = new Student("张三",18);
Student stu2 =new Student("李四",19);
Student stu3 = new Student("王麻子",20);
Student stu4 = new Student("张铁蛋",21);
//添加元素
link1.add(stu1);
link1.add(stu2);
link1.add(stu3);
link1.add(stu4);
System.out.println("元素个数:"+link1.size());
System.out.println("数组输出:"+link1.toString());
//删除元素
System.out.println("--------删除元素----------");
link1.remove(3);
System.out.println("--------删除下标为3的元素----------");
System.out.println("删除后元素个数:"+link1.size());
System.out.println("数组输出:"+link1.toString());
//link1.clear();清空
//遍历
//1.for循环
System.out.println("-------------for循环-------------");
for (int i = 0; i <link1.size() ; i++) {
System.out.println(link1.get(i));
}
//2.增强for循环
System.out.println("------.增强for循环---------");
for (Object o : link1) {
Student j =(Student) o;
System.out.println(j.toString());
}
//3.使用迭代器 4.使用列表迭代器。
}
}
Student类:
package com.tiedandan.集合.LinkedList使用;
public class Student {
public Student() {
}
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
元素个数:4
数组输出:[Student{name='张三', age=18}, Student{name='李四', age=19}, Student{name='王麻子', age=20}, Student{name='张铁蛋', age=21}]
--------删除元素----------
--------删除下标为3的元素----------
删除后元素个数:3
数组输出:[Student{name='张三', age=18}, Student{name='李四', age=19}, Student{name='王麻子', age=20}]
-------------for循环-------------
Student{name='张三', age=18}
Student{name='李四', age=19}
Student{name='王麻子', age=20}
------.增强for循环---------
Student{name='张三', age=18}
Student{name='李四', age=19}
Student{name='王麻子', age=20}