一.UML
①集合类图
②线下教育平台用例图
二.List集合特点
①学集合框架就是了解容器的数据结构(增删改查)
②有序的 可重复的
public static void main(String[] args) { List list = new ArrayList<>(); //增加 list.add("a"); list.add("b"); list.add("c"); ① foreach for (Object object : list) { System.out.println(object); } ② iterator 迭代器 Iterator it = list.iterator(); while(it.hasNext()) { System.out.println(it.next()); } ③ for for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } }
三.遍历方式
① foreach
② iterator 迭代器
③ for
public static void main(String[] args) { List list = new ArrayList<>(); //增加 list.add("a"); list.add("b"); list.add("c"); ① foreach for (Object object : list) { System.out.println(object); } ② iterator 迭代器 Iterator it = list.iterator(); while(it.hasNext()) { System.out.println(it.next()); } ③ for for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } }
四.LinkedList
①对比ArrayList是数据结构
Linkedlist: 链表 特点:查询修改慢,增加删除快
Arraylist: 数组 特点:查询修改快,增加删除慢
②堆栈 队列
/** * linkedList调优 * 元素存储在集合的过程 * ArrayList * 数组 长度不可变 * * 1.证明数据结构就是数组 * 2.为什么数组长度不可变,集合List长度可变 * * 增长因子(一次性扩容多少) 0.5倍 扩容1.5倍 1+0.5 * @author PC * */ public class Demo3 { public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { ArrayList al = new ArrayList<>(50); for (int i = 0; i < 100; i++) { al.add(i); System.out.println(i+"\r"); getCurrentArrLength(al); } } //获取ArrayList al对象底层数组的长度 private static void getCurrentArrLength(ArrayList al) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { Field f = al.getClass().getDeclaredField("elementData"); f.setAccessible(true); Object[] Object = (java.lang.Object[]) f.get(al); System.out.println("当前集合底层数组的容器长度"+Object.length); }
五.增长因子论证
/** * linkedList调优 * 元素存储在集合的过程 * ArrayList * 数组 长度不可变 * * 1.证明数据结构就是数组 * 2.为什么数组长度不可变,集合List长度可变 * * 增长因子(一次性扩容多少) 0.5倍 扩容1.5倍 1+0.5 * @author PC * */ public class Demo3 { public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { ArrayList al = new ArrayList<>(50); for (int i = 0; i < 100; i++) { al.add(i); System.out.println(i+"\r"); getCurrentArrLength(al); } } //获取ArrayList al对象底层数组的长度 private static void getCurrentArrLength(ArrayList al) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { Field f = al.getClass().getDeclaredField("elementData"); f.setAccessible(true); Object[] Object = (java.lang.Object[]) f.get(al); System.out.println("当前集合底层数组的容器长度"+Object.length); }
六.集合框架ArrayList中的重复元素去重及其底层原理
/** * list底层对象去重原理 跟equals * @author PC * */ public class Demo4 { public static void main2(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { List list = new ArrayList<>(); list.add("a"); list.add("b"); list.add("c"); System.out.println("目前集合容器中的"+list); //去重 if(!list.contains("b")) { list.add("b"); } System.out.println("目前集合容器中的"+list); } public static void main(String[] args) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { List list = new ArrayList<>(); list.add(new Student(1,"zs")); list.add(new Student(2,"ls")); list.add(new Student(3,"lx")); System.out.println("目前集合容器中的"+list); //去重 if(!list.contains(new Student(3,"lx"))) { list.add(new Student(3,"lx")); } System.out.println("目前集合容器中的"+list); } } class Student{ private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Student() { // TODO Auto-generated constructor stub } public Student(int id, String name) { super(); this.id = id; this.name = name; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + "]"; } @Override public boolean equals(Object obj) { System.out.println("被调了。。。"); if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (id != other.id) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; }