一. List 的一些功能
import java.util.ArrayList; import java.util.Iterator; import java.util.List; /* * void add(int index,Object element) //在指定索引位置添加元素 Object remove(int index) //删除指定索引的元素,返回删除的元素 Object get(int index) //获取指定索引的元素 Object set(int index,Object element) //修改指定索引的元素,返回修改前的元素 ListIterator listIterator() //List特有的一种迭代器 */ public class ListDemo { public static void main(String[] args) { List l = new ArrayList(); l.add("hello"); l.add("world"); l.add("java"); // l.add("javaee"); // l.add("haha"); //void add(int index,Object element) //在指定索引位置添加元素 //l.add(1, "javaee"); //java.lang.IndexOutOfBoundsException //l.add(11,"haha"); //Object remove(int index) //删除指定索引的元素 // System.out.println("remove:"+l.remove(2)); // System.out.println("remove:"+l.remove(3)); //java.lang.IndexOutOfBoundsException //System.out.println("remove:"+l.remove(11)); //Object get(int index) //获取指定索引的元素 // System.out.println("get:"+l.get(1)); // System.out.println("get:"+l.get(3)); //java.lang.IndexOutOfBoundsException //System.out.println("get:"+l.get(11)); //Object set(int index,Object element) //修改指定索引的元素,返回修改前的元素 System.out.println("set:"+l.set(1, "world11")); System.out.println("set:"+l.set(1, "world")); System.out.println("l:"+l); // Iterator it = l.iterator(); // while(it.hasNext()){ // String s = (String)it.next(); // System.out.println(s); // } } }
二.List 字符串的遍历
(1)可以用Collection中的Iterator it = l.iterator();这个方法去遍历、
import java.util.ArrayList; import java.util.Iterator; import java.util.List; /* * List存储字符串并遍历 */ public class ListDemo { public static void main(String[] args) { List l = new ArrayList(); l.add("hello"); l.add("world"); l.add("java"); System.out.println("l:"+l); Iterator it = l.iterator(); while(it.hasNext()){ String s = (String)it.next(); System.out.println(s); } } }
(2)List的for循环遍历方式。通过get()方法和size()方法实现的
/* * List的for循环遍历方式。通过get()方法和size()方法实现的 */ import java.util.ArrayList; import java.util.List; public class ListDemo2 { public static void main(String[] args) { List l = new ArrayList(); l.add("hello"); l.add("world"); l.add("java"); l.add("javaee"); l.add("haha"); System.out.println(l.get(0)); System.out.println(l.get(1)); System.out.println(l.get(2)); System.out.println(l.get(3)); System.out.println(l.get(4)); //System.out.println(l.get(5)); System.out.println("----------------"); // for(int i=0;i<5;i++){ // System.out.println(l.get(i)); // } //List的for循环遍历方式。通过get()方法和size()方法实现的 for(int i=0;i<l.size();i++){ System.out.println(l.get(i)); } } }
(3)List特有的迭代器
三.List特有的迭代器
有独特的功能相比较Coolection 中的 就是可以倒序输出 有两个注意点
(1)必须进行正序遍历后才可以逆序
(2)逆序调用的那个用的是同一个用来接收的接口的那个 不可以在新建一个来接受 然后调用。
这个地方是看指针的 逆序需要判断箭头前有没有东西
import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.ListIterator; /* * List特有的迭代器 * ListIterator listIterator() */ public class ListDemo { public static void main(String[] args) { List l = new ArrayList(); l.add("hello"); l.add("world"); l.add("java"); ListIterator li = l.listIterator(); //返回子类对象 // while(li.hasNext()){ // String s = (String)li.next(); // System.out.println(s); // } System.out.println("----------------------"); // System.out.println("previous:"+li.previous()); // System.out.println("previous:"+li.previous()); // System.out.println("previous:"+li.previous()); while(li.hasPrevious()){ System.out.println("previous:"+li.previous()); } System.out.println("----------------------"); Iterator it = l.iterator(); while(it.hasNext()){ String s = (String)it.next(); System.out.println(s); } } }四.遇见world添加东西 的那个案例
import java.util.ArrayList; import java.util.List; import java.util.ListIterator; /* * 需求:判断集合中是否有"world",如果有就添加"javaee" * * java.util.ConcurrentModificationException * 当方法检测到对象的并发修改,但不允许这种修改时,抛出此异常。 * 产生的原因: * 迭代器是依赖于集合而存在的,在判断成功后,集合的中新添加了元素,而迭代器却不知道,所以就报错了,这个错叫并发修改异常。 * 其实这个问题描述的是:迭代器遍历元素的时候,通过集合是不能修改元素的。 * 如何解决呢? * 解决: * 方案1: * 用迭代器遍历,用迭代器修改 * 元素是跟在刚才迭代的元素后面的。 * 方案2: * 用for循环遍历,用集合修改 * 元素在最后添加的。 */ public class ListDemo2 { public static void main(String[] args) { List l = new ArrayList(); l.add("hello"); l.add("world"); l.add("java"); // ListIterator lit = l.listIterator(); // while(lit.hasNext()){ // String s = (String)lit.next(); // if(s.equals("world")){ // l.add("javaee"); // } // } // ListIterator lit = l.listIterator(); // while(lit.hasNext()){ // String s = (String)lit.next(); // if(s.equals("world")){ // lit.add("javaee"); // } // } //l:[hello, world, javaee, java] for(int i=0;i<l.size();i++){ String s = (String)l.get(i); if(s.equals("world")){ l.add("javaee"); } } //l:[hello, world, java, javaee] System.out.println("l:"+l); } }
这两种办法还不太一样 迭代器弄得直接在后面跟着添加 因为是看指针的 走到哪里加到哪里
for 集合这个 add是直接加载在后面的 碰见一个world就在最后加一个
(1)迭代器遍历 就用迭代器修改
(2)集合遍历就用集合修改
五.学生案例
记住要重写tostring
import java.util.ArrayList; import java.util.List; import java.util.ListIterator; /* * 需求:用集合存储学生,把成绩60分以下的删除 */ public class ListTest { public static void main(String[] args) { List l = new ArrayList(); Student s1 = new Student("张一",80); Student s2 = new Student("张二",90); Student s3 = new Student("张三",70); Student s4 = new Student("张四",40); Student s5 = new Student("张五",30); Student s6 = new Student("张六",50); Student s7 = new Student("张七",56); Student s8 = new Student("张八",68); l.add(s1); l.add(s2); l.add(s3); l.add(s4); l.add(s5); l.add(s6); l.add(s7); l.add(s8); System.out.println("l:"+l); ListIterator lit = l.listIterator(); while(lit.hasNext()){ Student s = (Student)lit.next(); if(s.getCj() < 60){ lit.remove(); } } System.out.println("l:"+l); } }
public class Student { private String name; private int cj; public Student() { super(); } public Student(String name, int cj) { super(); this.name = name; this.cj = cj; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getCj() { return cj; } public void setCj(int cj) { this.cj = cj; } @Override public String toString() { return "Student [name=" + name + ", cj=" + cj + "]"; } }
六.List的三个实现类 子类
(1)LinkedList类
import java.util.LinkedList; /* * LinkedList类概述 底层数据结构是链表,查询慢,增删快 线程不安全,效率高 LinkedList类特有功能 public void addFirst(Object e) addLast(Object e) public Object getFirst() getLast() public Object removeFirst() public Object removeLast() */ public class LinkedListDemo { public static void main(String[] args) { LinkedList lk = new LinkedList(); lk.add("hello"); lk.add("world"); lk.add("java"); // lk.addFirst("haha"); // lk.addLast("javaee"); // // System.out.println("getFirst:"+lk.getFirst()); // System.out.println("getLast:"+lk.getLast()); // // System.out.println("removeFirst:"+lk.removeFirst()); // System.out.println("removeLast:"+lk.removeLast()); System.out.println("pool:"+lk.poll()); System.out.println("pool:"+lk.poll()); System.out.println("pool:"+lk.poll()); System.out.println("lk:"+lk); } }
(2)Vector类
import java.util.Enumeration; import java.util.Iterator; import java.util.Vector; /* * Vector类概述 底层数据结构是数组,查询快,增删慢 线程安全,效率低 Vector类特有功能 public void addElement(Object obj) //add() public Object elementAt(int index) //get() public Enumeration elements() //迭代器 */ public class VectorDemo { public static void main(String[] args) { Vector v = new Vector(); v.addElement("hello"); v.addElement("world"); v.addElement("java"); System.out.println("v:"+v); Enumeration e = v.elements(); while(e.hasMoreElements()){ String s = (String)e.nextElement(); System.out.println(s); } System.out.println("----------------"); Iterator it = v.iterator(); while(it.hasNext()){ String s = (String)it.next(); System.out.println(s); } System.out.println("--------------"); for(int i=0;i<v.size();i++){ String s = (String)v.get(i); System.out.println(s); } } }
(3) ArrayList类
import java.util.ArrayList; import java.util.ListIterator; /* * ArrayList类概述 底层数据结构是数组,查询快,增删慢 线程不安全,效率高 构造方法: ArrayList() ArrayList(Collection c) 案例: 存储字符串,并遍历 */ public class ArrayListDemo { public static void main(String[] args) { //List l = new ArrayList(); ArrayList al = new ArrayList(); al.add("hello"); al.add("world"); al.add("java"); // ListIterator li = al.listIterator(); // while(li.hasNext()){ // String s = (String)li.next(); // System.out.println(s); // } ArrayList al2 = new ArrayList(al); System.out.println("al2:"+al2); } }
七.ArrayList类案例
(1)去除重复的字符串 两种方式
一种是创建个集合一种是equals方法比较 需要注意的是String中重写了equals方法和tostring方法
import java.util.ArrayList; /* * 去除集合中字符串的重复值(字符串的内容相同) */ public class ArrayListTest { public static void main(String[] args) { ArrayList al = new ArrayList(); al.add("hello"); al.add("world"); al.add("java"); al.add("javaee"); al.add("haha"); al.add("world"); al.add("java"); al.add("javaee"); al.add("haha"); al.add("haha"); al.add("world"); //定义一个新的集合 ArrayList al2 = new ArrayList(); //遍历需要去重复的集合 for(int i=0;i<al.size();i++){ String s = (String)al.get(i); //判断新集合里面是否包含此元素,如果不包含就添加到新集合中 if(!al2.contains(s)){ al2.add(s); } } System.out.println("al2:"+al2); } }
import java.util.ArrayList; /* * 去除集合中字符串的重复值(字符串的内容相同) 要求:不创建新集合 */ public class ArrayListTest1 { public static void main(String[] args) { ArrayList al = new ArrayList(); al.add("hello"); al.add("world"); al.add("java"); al.add("javaee"); al.add("haha"); al.add("world"); al.add("java"); al.add("javaee"); al.add("haha"); al.add("haha"); al.add("world"); for(int i=0;i<al.size()-1;i++){ for(int j = i+1;j<al.size();j++){ String s = (String)al.get(i); String s1 = (String)al.get(j); if(s.equals(s1)){ al.remove(j); j--; } } } System.out.println("al:"+al); } }
(2)学生 去除重复的案例 需要去重写equals方法和tostring方法才能行 要不然全部原样输出
import java.util.ArrayList; import java.util.ListIterator; public class ArrayListDemo { public static void main(String[] args) { ArrayList s = new ArrayList(); Student c1 = new Student("张1",21); Student c2 = new Student("张2",22); Student c3 = new Student("张3",23); Student c4 = new Student("张4",24); Student c5 = new Student("张1",21); Student c6 = new Student("张5",25); Student c7 = new Student("张3",23); Student c8 = new Student("张3",23); Student c9 = new Student("张2",22); s.add(c1); s.add(c2); s.add(c3); s.add(c4); s.add(c5); s.add(c6); s.add(c7); s.add(c8); s.add(c9); s.add(c1); for(int i=0;i<s.size()-1;i++){ for(int j = i+1;j<s.size();j++){ Student m = (Student)s.get(i); Student n = (Student)s.get(j); if(m.equals(n)){ s.remove(j); j--; } } } System.out.println(s); } }
public class Student { private String name; private int age; public Student() { super(); // TODO Auto-generated constructor stub } public Student(String name, int age) { super(); 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 int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (age != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } }
(2)上面是用的for循环比较 equals
这里再用两个集合 一个是空的没有就加 也要重写那两个方法
import java.util.ArrayList; import java.util.ListIterator; public class ArrayListDemo { public static void main(String[] args) { ArrayList s = new ArrayList(); Student c1 = new Student("张1",21); Student c2 = new Student("张2",22); Student c3 = new Student("张3",23); Student c4 = new Student("张4",24); Student c5 = new Student("张1",21); Student c6 = new Student("张5",25); Student c7 = new Student("张3",23); Student c8 = new Student("张3",23); Student c9 = new Student("张2",22); s.add(c1); s.add(c2); s.add(c3); s.add(c4); s.add(c5); s.add(c6); s.add(c7); s.add(c8); s.add(c9); s.add(c1); ArrayList s1 = new ArrayList(); for(int i = 0;i<s.size();i++){ Student x = (Student)(s.get(i)); if(!s1.contains(x)){ s1.add(x); } } System.out.println(s1); } }八.
List:(面试题List的子类特点)
ArrayList:
底层数据结构是数组,查询快,增删慢。
线程不安全,效率高。
Vector:
底层数据结构是数组,查询快,增删慢。
线程安全,效率低。
LinkedList:
底层数据结构是链表,查询慢,增删快。
线程不安全,效率高。
List有三个儿子,我们到底使用谁呢?
看需求(情况)。
要安全吗?
要:Vector(即使要安全,也不用这个了,后面有替代的)
不要:ArrayList或者LinkedList
查询多:ArrayList
增删多:LinkedList
如果你什么都不懂,就用ArrayList。