集合框架之List

来源:互联网 发布:js鼠标拖动事件 编辑:程序博客网 时间:2024/04/30 11:21
------ android培训java培训,期待与您交流 ------
黑马程序员 java集合框架之List
集合框架:用于存储数据的容器。
特点:
1:对象封装数据,对象多了也需要存储,集合用于存储对象。
2:对象的个数确定可以使用数组,不确定用集合,因为集合是可变长度的。
集合和数组的区别:
1:数组是固定长度的,集合可变长度的。
2:数组可以存储基本数据类型,也可以存储引用数据类型,集合只能存储引用数据类型。
3:数组存储的元素必须是同一个数据类型,集合存储的对象可以是不同数据类型。
Collection接口两种常用体系结构:
Collection:
|--List:有序(元素存入集合的顺序和取出的顺序一致),元素都有索引,元素可以重复。
|--Set:无序(存入和取出顺序有可能不一致),不可以存储重复元素,必须保证元素唯一性。
collection中的基本方法:
1,添加: boolean add(E e) :添加一个元素
boolean addAll(Collection<? extends E> c)  :添加一个集合中的所有元素。
2,删除: void clear() :将集合中的元素全删除,清空集合。
boolean remove(Object o) :删除集合中指定的对象。
boolean removeAll(Collection<?> c) :删除部分元素。部分元素和传入Collection一致。
3,判断: boolean contains(obj) :集合中是否包含指定元素 。
boolean containsAll(Collection) :集合中是否包含指定的多个元素。
boolean isEmpty():集合中是否有元素。 
4,获取:int size():返回此 collection 中的元素数。
5,取交集:boolean  retainAll(Collection) :对当前集合中保留和指定集合中的相同的元素,如果两个集合元素相同,
  返回flase;如果retainAll修改了当前集合,返回true。
6,获取集合中所有元素,Iterator<E> iterator(),返回在此 collection 的元素上进行迭代的迭代器。 
7,将集合变成数组: Object[] toArray(),返回包含此 collection 中所有元素的数组。 
Iterator接口:
每一个集合都有自己的数据结构,都有特定的取出自己内部元素的方式。为了便于操作所有的容器,取出元素,将容器内部的取出。
式按照一个统一的规则向外提供,这个规则就是Iterator接口。
Iterator it = coll.iterator();
从AIP中可以看到,collection提供的接口只有三个方法:
boolean hasNext()  如果仍有元素可以迭代,则返回 true。 
E next()   返回迭代的下一个元素。 
void remove()     从迭代器指向的 collection 中移除迭代器返回的最后一个元素(可选操作)。 
也就是说在遍历元素的时候,只能做移除操作,这是由局限性的,但在其子类中ListIterator得到了解决。
collection的List接口:
List本身是Collection接口的子接口,具备了Collection的所有方法,List的数据结构是数组,其特有方法都有索引,这是该集合最
大的特点。
List:有序,元素都有索引,元素可以重复。
|--ArrayList:底层的数据结构是数组,线程不同步,ArrayList替代了Vector,查询元素的速度非常快。
|--LinkedList:底层的数据结构是链表,线程不同步,增删元素的速度非常快。
|--Vector:底层的数据结构就是数组,线程同步的,Vector无论查询和增删都很慢,一般用的较少。
1,添加:boolean add(E e) :向列表的尾部添加指定的元素。
void add(int index, E element) 在列表的指定位置插入指定元素。 
2,删除:E remove(int index) 移除列表中指定位置的元素,返回被删的元素,这个方法注意是有返回值的。
3,获取:E get(int index) 返回列表中指定位置的元素。
int indexOf(Object o) 返回此列表中第一次出现的指定元素的索引,如果此列表不包含该元素,则返回 -1。 
        int lastIndexOf(Object o) 返回此列表中最后出现的指定元素的索引,如果列表不包含此元素,则返回 -1。 
List<E> subList(int fromIndex, int toIndex) 返回列表中指定的 fromIndex 和 toIndex 之间的部分
4,修改:E set(int index, E element) 用指定元素替换列表中指定位置的元素。 
5,获取所有元素:ListIterator listIterator():list集合特有的迭代器,该迭代器增删改查方法都具备。
API中提供的ListIterator具有的方法:
boolean hasPrevious()   如果以逆向遍历列表,列表迭代器有多个元素,则返回 true。 
int nextIndex() 返回对 next 的后续调用所返回元素的索引。 
E previous()   返回列表中的前一个元素。 
int previousIndex() 返回对 previous 的后续调用所返回元素的索引。 
void remove() 从列表中移除由 next 或 previous 返回的最后一个元素。 
void set(E e) 用指定元素替换 next 或 previous 返回的最后一个元素(可选操作)。 
List接口的实现类LinkedList的特有方法:
void addFirst(E e) 将指定元素插入此列表的开头。
void addLast(E e) 将指定元素添加到此列表的结尾。 
E getFirst() 返回此列表的第一个元素。 
E getLast() 返回此列表的最后一个元素。 
E removeFirst()         移除并返回此列表的第一个元素。 
E removeLast() 移除并返回此列表的最后一个元素。 
在jdk1.6以后,出现了新的增加,获取和移除方法:
boolean offerFirst(E e) 在此列表的开头插入指定的元素。 
boolean offerLast(E e)  在此列表末尾插入指定的元素。  
E peekLast() 获取但不移除此列表的最后一个元素;如果此列表为空,则返回 null。   
E peekLast() 获取但不移除此列表的最后一个元素;如果此列表为空,则返回 null。
E pollFirst() 获取并移除此列表的第一个元素;如果此列表为空,则返回 null。 
E pollLast() 获取并移除此列表的最后一个元素;如果此列表为空,则返回 null。
List的示例,去除重复元素:
package collection.list;
import java.util.ArrayList;
import java.util.ListIterator;
public class ArraylistDemo {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
ArrayList al=new ArrayList();
al.add("java");
al.add(".net");
al.add(".net");
al.add("ios");
al.add("android");
ArrayList myList= new ArrayList();
for(ListIterator lit=al.listIterator();lit.hasNext();){
Object obj=lit.next();
if(!myList.contains(obj)){
myList.add(obj);
}
}
System.out.println(myList);
}
}
上面的对象都是字符串,contains()方法底层调用的是equals()方法,但从Object类继承过来的该方法比较的是对象的hashcode,
不具有针对性,因此自定义对象时,我们需要根据自己的要求复写该方法,建立自己的比较方式。
如自定义对象学生,姓名年龄相同视为同一个学生,去除重复元素:
package collection.list;
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListTest2 {
@SuppressWarnings("unchecked")
public static void main(String[] args) {
ArrayList al=new ArrayList();
al.add(new Student("zhangwuji",22));
al.add(new Student("gaoyuanyuan",34));
al.add(new Student("yangguo",28));
al.add(new Student("zhangwuji",22));
ArrayList list =noDuplication(al);
Iterator it=list.iterator();
while(it.hasNext()){
Student stu=(Student)it.next();
System.out.println(stu.getName()+"..."+stu.getAge());
}
}
//自定义对象,如学生,将同姓名同年龄的人视为同一个元素,去除重复元素
@SuppressWarnings("unchecked")
public static ArrayList noDuplication(ArrayList al){//该方法在set中被封装了
ArrayList tempAl=new ArrayList();
Iterator it=al.iterator();
while(it.hasNext()){
Student stu=(Student)it.next();
System.out.println(stu.getName()+"....."+stu.getAge());
if(!tempAl.contains(stu)){//这里底层会自动调用复写的equals();
tempAl.add(stu);
}
}
return tempAl;
}
}
class Student{
private String name;
private int age;
Student(String name,int age){
this.name=name;
this.age=age;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return age;
}
//在javabean中建立自己的比较方式,复写Object类的equals方法
public boolean equals(Object obj){
if(!(obj instanceof Student)){
throw new ClassCastException();
}
Student stu =(Student)obj;
System.out.println(this.name+"..."+stu.name);
return this.name.equals(stu.name)&&this.age==stu.age;//按姓名年龄定义的比较方式
}
}
------ android培训java培训,期待与您交流 ------
0 0
原创粉丝点击