List自己是Collection接口的子接口,具有了Collection的全部方法。ListIterator是List集合特有的迭代器。ListIterator it = list.listIterator;//取代Iterator it = list.iterator;java
List:有序(元素存入集合的順序和取出的順序一致),元素都有索引。元素能夠重複。數組
|--ArrayList:底層的數據結構是數組,線程不一樣步,ArrayList替代了Vector,查詢元素的速度很是快。數據結構
|--LinkedList:底層的數據結構是鏈表,線程不一樣步,增刪元素的速度很是快。ide
|--Vector:底層的數據結構就是數組,線程同步的,Vector不管查詢和增刪都巨慢。this
可變長度數組的原理:spa
當元素超出數組長度,會產生一個新數組,將原數組的數據複製到新數組中,再將新的元素添加到新數組中。線程
ArrayList:是按照原數組的50%延長。構造一個初始容量爲 10 的空列表。code
Vector:是按照原數組的100%延長。對象
注意:對於list集合,底層判斷元素是否相同,其實用的是元素自身的equals方法完成的。因此建議元素都要複寫equals方法,創建元素對象本身的比較相同的條件依據。blog
1,ArrayList遍歷:
import java.util.*;
public classTest{ public static void main(String[] args) { List<String> list=new ArrayList<String>(); list.add("Hello"); list.add("World"); list.add("HAHAHAHA"); //第一種遍歷方法使用foreach遍歷List for (String str : list) { //也能夠改寫for(int i=0;i<list.size();i++)這種形式 System.out.println(str); } //第二種遍歷,把鏈表變爲數組相關的內容進行遍歷 String[] strArray=new String[list.size()]; list.toArray(strArray); for(int i=0;i<strArray.length;i++) //這裏也能夠改寫爲 foreach(String str:strArray)這種形式 { System.out.println(strArray[i]); } //第三種遍歷 使用迭代器進行相關遍歷 Iterator<String> ite=list.iterator(); while(ite.hasNext())//判斷下一個元素以後有值 { System.out.println(ite.next()); } } }
2,ArrayList去除集合中字符串的重複值
package com.hpioneer.collection;
import java.util.ArrayList;
import java.util.Iterator;
/**
* @Description:
* @Author: HPioneer
* @CreateTime: 2018/5/7 19:03
* @File: Demo_ArrayLits of JavaProject in com.hpioneer.collection
* @FullFileName: com.hpioneer.collection.Demo_ArrayLits
* @Create By IntelliJ
* @Version: 1.0
*/
public class Demo_ArrayList { public static void main(String[] args) { ArrayList list = new ArrayList(); list.add("a"); list.add("a"); list.add("b"); list.add("b"); list.add("c"); list.add("c"); list.add("c"); list.add("c"); ArrayList newList = getSingle(list); System.out.println(newList); } public static ArrayList getSingle(ArrayList list) { ArrayList newList = new ArrayList<>(); //1,建立新集合 Iterator it = list.iterator(); //2,根據傳入的集合(老集合)獲取迭代器 while(it.hasNext()) { //3,遍歷老集合 Object obj = it.next(); //記錄住每個元素 if(!newList.contains(obj)) { //若是新集合中不包含老集合中的元素 newList.add(obj); //將該元素添加 } } return newList; } }
3,LinkedList模擬棧
package com.hpioneer.collection; import java.util.LinkedList; import java.util.Stack; /** * @Description: * @Author: HPioneer * @CreateTime: 2018/5/8 21:14 * @File: Demo_LinkedList of JavaProject in com.hpioneer.collection * @FullFileName: com.hpioneer.collection.Demo_LinkedList * @Create By IntelliJ * @Version: 1.0 */ public class Demo_LinkedList { public static void main(String[] args) { LinkedList list = new LinkedList(); list.addFirst("a"); list.addFirst("b"); list.addFirst("c"); list.addFirst("d"); list.addLast("e"); System.out.println(list.get(0)); System.out.println(list); while (!list.isEmpty()){ System.out.println(list.removeLast()); } } }
4,ArrayList嵌套ArrayList
package com.hpioneer.collection; import java.util.ArrayList; /** * @Description: * @Author: HPioneer * @CreateTime: 2018/5/8 21:24 * @File: Demo_ArrayLists of JavaProject in com.hpioneer.collection * @FullFileName: com.hpioneer.collection.Demo_ArrayLists * @Create By IntelliJ * @Version: 1.0 */ public class Demo_ArrayLists { public static void main(String[] args) { ArrayList<ArrayList<Person>> list = new ArrayList<>(); ArrayList<Person> first = new ArrayList<>(); //建立第一個班級 first.add(new Person("楊冪", 30)); first.add(new Person("李冰冰", 33)); first.add(new Person("范冰冰", 20)); ArrayList<Person> second = new ArrayList<>(); second.add(new Person("黃曉明", 31)); second.add(new Person("趙薇", 33)); second.add(new Person("陳坤", 32)); //將班級添加到學科集合中 list.add(first); list.add(second); System.out.println(first); System.out.println(second); //遍歷學科集合 for(ArrayList<Person> a : list) { for(Person p : a) { System.out.println(p); } } } }
引用的Person類爲:
package com.hpioneer.collection; /** * @Description: * @Author: HPioneer * @CreateTime: 2018/5/8 21:25 * @File: Person of JavaProject in com.hpioneer.collection * @FullFileName: com.hpioneer.collection.Person * @Create By IntelliJ * @Version: 1.0 */ public class Person { private String name; private int age; public Person() { super(); } public Person(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 "Person [name=" + name + ", age=" + age + "]"; } /*@Override public boolean equals(Object obj) { Person p = (Person)obj; return this.name.equals(p.name) && this.age == p.age; }*/ }