集合的特点
1.具备容器基本特点增删改查
2.有序
3.元素可重复
list集合的遍历方式
1.foreach增强for循环
2.迭代器
3.fori通过下标遍历
package collaction; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class demo1 { public static void main(String[] args) { List list=new ArrayList<>(); list.add("a"); list.add("b"); //foreach for (Object object : list) { System.out.println(object); } //迭代器 Iterator it=list.iterator(); while(it.hasNext()) { System.out.println(it.next()); } //fori for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } }
list的子类Arraylist和Linkedlist的区别
Arraylis:数组(如图下)
Linkedlist:链表(如图下)
用linkedList完成一个堆栈容器
package collaction; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; import java.util.List; public class demo2 { public static void main(String[] args) { LinkedList ll=new LinkedList<>(); ll.add("a"); ll.add("b"); ll.add("c"); DuiZhan dz=new DuiZhan(ll); //定义一个方法,当这个方法被调用的时候, //后存进去的元素要先输出,先存进去的元素要后输出 System.out.println(dz.pop()); System.out.println(dz.pop()); System.out.println(dz.pop()); } } class DuiZhan{ private LinkedList ll; public DuiZhan(LinkedList ll) { super(); this.ll = ll; } //压栈push 弹栈pop public Object pop(){ return ll.removeLast(); } }
增长因子论证(list调优)
1.证明数据结构就是数组
2.为什么数组长度不可变,集合List长度可变
import java.util.ArrayList; //底层数组的增长因子为0.5,扩容为1.5倍 public class demo3 { public static void main(String[] args) throws Exception { ArrayList al =new ArrayList<>(50); //50指第一次扩容的数 for (int i = 0; i < 100; i++) { al.add(i); System.out.println(i+"\r"); getCurrentArrayLength(al); } } //获取ArrayList al对象底层数组的长度 private static void getCurrentArrayLength(ArrayList al) throws Exception{ // TODO Auto-generated method stub Field f = al.getClass().getDeclaredField("elementData"); f.setAccessible(true); Object[] object =(Object[]) f.get(al); System.out.println("当前集合底层数组的容器长度:"+object.length); } }
list去重原理(重写equals;字符串不需要担心会重复)
package collaction; import java.util.ArrayList; import java.util.List; public class demo4 { public static void main(String[] args) { List list = new ArrayList(); list.add(new Student(1, "zs")); list.add(new Student(2, "ls")); list.add(new Student(3, "nb")); System.out.println("目前集合容器中的元素:"+list); if (!list.contains(new Student(3, "nb"))){ list.add(new Student(3, "nb"));} 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; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + "]"; } public Student(int id, String name) { super(); this.id = id; this.name = name; } public Student() { super(); // TODO Auto-generated constructor stub } @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; } }