LinkedList
链表结构实现,增删快,查询慢
package jihe; import java.util.Iterator; import java.util.LinkedList; import java.util.ListIterator; /** * LinkedList的使用 * 存储结构:双向链表 */ public class Demo07 { public static void main(String[] args) { //创建集合 LinkedList linkedList = new LinkedList(); //1.添加元素 Student s1 = new Student("小A",20); Student s2 = new Student("小B",22); Student s3 = new Student("小C",24); linkedList.add(s1); linkedList.add(s2); linkedList.add(s3); System.out.println("元素个数:"+linkedList.size()); System.out.println(linkedList.toString()); //2.删除 //linkedList.remove(0); //System.out.println("删除之后:"+linkedList.size()); //3.遍历 //3.1for遍历 System.out.println("--------3.1for--------"); for (int i = 0; i < linkedList.size(); i++) { System.out.println(linkedList.get(i)); } //3.2增强for System.out.println("--------3.2增强for--------"); for (Object object : linkedList) { System.out.println(object); } //3.3使用迭代器 System.out.println("--------3.3使用迭代器--------"); Iterator it = linkedList.iterator(); while (it.hasNext()){ Student s = (Student)it.next(); System.out.println(s.toString()); } //3.4使用列表迭代器 System.out.println("--------3.4使用列表迭代器--------"); ListIterator lit = linkedList.listIterator(); while (lit.hasNext()){ Student s = (Student)lit.next(); System.out.println(s.toString()); } //4.判断 System.out.println(linkedList.contains(s1)); System.out.println(linkedList.isEmpty()); //5.获取 System.out.println(linkedList.indexOf(s2)); } }
package jihe; import java.util.Objects; public class Student { private String name; private int age; public Student() { } 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; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return age == student.age && Objects.equals(name, student.name); } @Override public int hashCode() { return Objects.hash(name, age); } }
搜索
复制
标签:LinkedList,System,name,println,Student,集合,out,linkedList From: https://www.cnblogs.com/sususyq-/p/16637144.html