集合框架(List)

来源:互联网 发布:我在百度云申请了域名 编辑:程序博客网 时间:2024/04/30 15:57

List接口:

List本身是Collection接口的子接口,具备了Collection的所有方法。现在学习List体系特有的共性方法,查阅方法发现List的特有方法都有索引,这是该集合最大的特点。

List:有序(元素存入集合的顺序和取出的顺序一致),元素都有索引。元素可以重复。

|--ArrayList:底层的数据结构是数组,线程不同步,ArrayList替代了Vector,查询元素的速度非常快。

|--LinkedList:底层的数据结构是链表,线程不同步,增删元素的速度非常快。

|--Vector:底层的数据结构就是数组,线程同步的,Vector无论查询和增删都巨慢。

 

1,添加:

add(index,element) :在指定的索引位插入元素。

addAll(index,collection) :在指定的索引位插入一堆元素。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. al.add(1,"hello bug");  

2,删除:

remove(index) :删除指定索引位的元素。 返回被删的元素。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. al.remove(2);  

3,获取:

Object get(index) :通过索引获取指定元素。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. al.get(2);  

int indexOf(obj) :获取指定元素第一次出现的索引位,如果该元素不存在返回-1;

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. al.indexOf("hello bug")  

所以,通过-1,可以判断一个元素是否存在。

int lastIndexOf(Object o) :反向索引指定元素的位置。

List subList(start,end) :获取子列表。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. al.subList(02);//包含开头不包含结尾  

4,修改:

Object set(index,element) :对指定索引位进行元素的修改。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. al.set(2,"hello victor");  

5,获取所有元素:

for循环遍历获取获取:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. for (int i = 0; i < al.size(); i++) {  
  2.     System.out.println(al.get(i));  
  3. }  


ListIterator是List集合特有的迭代器

ListIterator it = list.listIterator;//取代Iterator it = list.iterator;

在迭代时,不可以通过集合对象的方法操作集合中的元素。

因为会发生ConcurrentModificationException异常。

所以,在迭代器时,只能用迭代器的方法操作元素,可是Interator的方法时有限的,只能对元素进行判断,去除,删除的操作。

如果想要其他操作,如:添加,修改等,就需要使用其子接口Listlterator

该接口只能通过List集合的listIterrator方法获取。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ListIterator li = al.listIterator();  
  2.           
  3.         while(li.hasNext())  
  4.         {  
  5.             Object obj = li.next();  
  6.               
  7.             if(obj.equals("hello world"))  
  8.             {  
  9.                 li.add("hello bug");  
  10.             }  
  11.         }  
  12.           
  13.         System.out.println(al);  

hasPrevious() 如果以逆向遍历列表,列表迭代器有多个元素,则返回 true。

previous() 返回列表中的前一个元素。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. while(li.hasPrevious())  
  2.         {  
  3.             Object obj = li.previous();  
  4.               
  5.             if(obj.equals("hello bug"))  
  6.             {  
  7.                 li.set("hello 123");  
  8.             }  
  9.         }  
  10.         System.out.println(al);  


List下分支:

ArrayList: 底层的数据结构,使用的是数组结构。 特点:查询速度快,但是增删稍慢,线程同步。

LinkedList: 底层使用的链表数据结构。特点:增删的速度很快,查询的速度慢

Vector: 底层是数组数据结构被ArrayList所取代,线程同步。


Vector特有的取出方式:枚举。

枚举和迭代器很想,其实枚举和迭代器时一样的,因为枚举的名称以及方法的名称过长,所以渐渐被取代了。



LinkedList:

-addFirst()  将元素添加至集合开头

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. LinkedList link = new LinkedList();  
  2.   
  3. link.addFirst("hello world");  
  4. link.addFirst("hello java");  
  5. link.addFirst("hello china");  
  6.   
  7. System.out.println(link);//[hello china, hello java, hello world]  

-addLast()  将元素添加至集合末尾

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. LinkedList link = new LinkedList();  
  2.   
  3. link.addLast("hello world");  
  4. link.addLast("hello java");  
  5. link.addLast("hello china");  
  6.   
  7. System.out.println(link);//[hello world, hello java, hello china]  

-getFirst()  获取集合开头元素,但不移除元素

-getLast()  获取集合末尾元素,但不移除元素

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. LinkedList link = new LinkedList();  
  2.   
  3. link.addLast("hello world");  
  4. link.addLast("hello java");  
  5. link.addLast("hello china");  
  6.   
  7. System.out.println(link.getFirst());//hello world  
  8. System.out.println(link.getLast());//hello china  

-removeFirst() 移除并返回集合开头元素。如果集合中没有元素,会出现异常。该方法为LinkedList特有方法,可以用来特殊遍历集合

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. while(!link.isEmpty())  
  2. {  
  3.     System.out.println(link.removeFirst());  
  4. }  

在JDK1.6以后出现了替代方法

如果集合中没有元素会返回NULL

getFirst()------>offerFirst()

getLast()------>offerLast()


getFirst()------>peekFirst()

getLast()------>peekLast()


LinkedList练习:

使用LinkedList模拟一个堆栈或者队列数据结果


堆栈:先进后出  如同一个杯子

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. class DuiLie  
  2. {  
  3.     private LinkedList link;  
  4.       
  5.     DuiLie()  
  6.     {  
  7.         link = new LinkedList();  
  8.     }  
  9.     public void myAdd(Object obj)  
  10.     {  
  11.         link.addFirst(obj);  
  12.     }  
  13.     public Object myGet()  
  14.     {  
  15.         return link.removeFirst();  
  16.     }  
  17.     public boolean isNull()  
  18.     {  
  19.         return link.isEmpty();  
  20.     }  
  21. }  
  22.   
  23.   
  24. public class LinkedListTest {  
  25.     public static void main(String[] args) {  
  26.         DuiLie dl = new DuiLie();  
  27.         dl.myAdd("java01");  
  28.         dl.myAdd("java02");  
  29.         dl.myAdd("java03");  
  30.         dl.myAdd("java04");  
  31.           
  32.         while(!dl.isNull())  
  33.         {  
  34.         System.out.println(dl.myGet());  
  35.         }  
  36.     }  
  37.   
  38. }  

队列:先进先出  如同一个水管

修改上述代码DuiLie类中的myGet方法为

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public Object myGet()  
  2. {  
  3.     return link.removeLast();  
  4. }  


ArrayList练习:

去除ArrayList集合中的重复元素。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class ArrayListTest {  
  2.     public static void main(String[] args) {  
  3.         ArrayList al = new ArrayList();  
  4.           
  5.         al.add("java01");  
  6.         al.add("java02");  
  7.         al.add("java01");  
  8.         al.add("java02");  
  9.         al.add("java01");  
  10.         al.add("java03");  
  11.         System.out.println(al);  
  12.         System.out.println(singleElement(al));  
  13.     }  
  14.       
  15.     public static ArrayList singleElement(ArrayList al)  
  16.     {  
  17.         ArrayList newAl = new ArrayList();  
  18.         Iterator it = al.iterator();  
  19.           
  20.         while(it.hasNext())  
  21.         {  
  22.             Object obj = it.next();  
  23.               
  24.             if(!newAl.contains(obj))  
  25.                 newAl.add(obj);  
  26.         }  
  27.         return newAl;  
  28.     }  
  29.   
  30. }  

2.将自定义对象作为元素存到ArrayList集合中,并去除重复元素

比如:存人对象,同姓名同年龄,视为同一个人,为重复元素。

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. import java.util.*;  
  2.   
  3. class Person  
  4. {  
  5.     private String name;  
  6.     private int age;  
  7.     Person(String name,int age)  
  8.     {  
  9.         this.name = name;  
  10.         this.age = age;  
  11.     }  
  12.     //覆盖父类Object中的equals方法  
  13.     public boolean equals(Object obj)  
  14.     {  
  15.         //判断obj是否为Person类  
  16.         if(!(obj instanceof Person))  
  17.             return false;  
  18.           
  19.         Person p = (Person)obj;  
  20.         return this.name.equals(p.name)&&this.age == p.age;  
  21.   
  22.     }  
  23.     public String getName()  
  24.     {  
  25.         return name;  
  26.     }  
  27.     public int getAge()  
  28.     {  
  29.         return age;  
  30.     }  
  31. }  
  32.   
  33. public class ArrayListTest2 {  
  34.   
  35.     public static void main(String[] args) {  
  36.         ArrayList al = new ArrayList();  
  37.         al.add(new Person("lisi01",30));  
  38.         al.add(new Person("lisi02",32));  
  39.         al.add(new Person("lisi02",32));  
  40.         al.add(new Person("lisi03",33));  
  41.         al.add(new Person("lisi04",35));  
  42.         al.add(new Person("lisi04",35));  
  43.           
  44.         al = singleElement(al);  
  45.           
  46.         Iterator it = al.iterator();  
  47.           
  48.         while(it.hasNext())  
  49.         {  
  50.             Person p = (Person)it.next();  
  51.             System.out.println(p.getName()+"--"+p.getAge());  
  52.         }  
  53.   
  54.     }  
  55.     public static ArrayList singleElement(ArrayList al)  
  56.     {  
  57.         ArrayList newAl = new ArrayList();  
  58.         Iterator it = al.iterator();  
  59.           
  60.         while(it.hasNext())  
  61.         {  
  62.             Object obj = it.next();  
  63.               
  64.             if(!newAl.contains(obj))  
  65.                 newAl.add(obj);  
  66.         }  
  67.         return newAl;  
  68.     }  
  69.   
0 0
原创粉丝点击