Java中的List

2017-05-17  本文已影响0人  Fighting_rain

List接口在Collection的基础上添加了大量的方法,使得可以在List的中间插入和移除元素。
有两种类型的List:

ArrayList的具体方法来看下面这个例子:

public class ListFeatures {
    public static void main(String[] args) {
        List<String> pets = new ArrayList<>();
        String[] arrs = {"Mouse","Dog","Cats","Pug","Manx"};
        pets.addAll(Arrays.asList(arrs));
        System.out.println("1: "+pets); // [Mouse, Dog, Cats, Pug, Manx]
        pets.add("Cymric");
        System.out.println("2: "+pets); // [Mouse, Dog, Cats, Pug, Manx, Cymric]
        System.out.println("3: "+pets.contains("Cymric")); //true
        pets.remove("Cymric");
        System.out.println("4: "+pets); // [Mouse, Dog, Cats, Pug, Manx]
        System.out.println("5: "+pets.get(2)); //5: Cats
        System.out.println("6: "+pets.indexOf("Cymric")); // -1 没有找到,因为已经remove了
        List<String> sub = pets.subList(1, 4);
        System.out.println("7: "+pets);     //7: [Mouse, Dog, Cats, Pug, Manx]
        System.out.println("sub: "+sub);    //sub: [Dog, Cats, Pug]
        System.out.println("8: "+pets.containsAll(sub));//8: true 参数是一个Collection
        Collections.sort(sub);
        System.out.println("sorted sublist"+sub);// sorted sublist[Cats, Dog, Pug] 排序后的
        List<String> copy = new ArrayList<String>(pets);
        sub = Arrays.asList(pets.get(1),pets.get(4));
        System.out.println("sub: "+sub); //sub: [Cats, Manx]
        copy.retainAll(sub);    //取交集
        System.out.println("copy:"+copy);//copy:[Cats, Manx]
        copy = new ArrayList<String>(pets);
        System.out.println("copy:"+copy);   //copy:[Mouse, Cats, Dog, Pug, Manx]
        copy.set(0, "Rat");
        System.out.println("copy:"+copy); //copy:[Rat, Cats, Dog, Pug, Manx]
        System.out.println("sub: "+sub); //sub: [Cats, Manx]
        copy.removeAll(sub);    
        System.out.println("copy:" + copy); //copy:[Mouse, Dog, Pug]
        copy.addAll(2,sub);
        System.out.println("copy:" + copy); //copy:[Mouse, Dog, Cats, Manx, Pug]
        System.out.println(pets.isEmpty());//false
        pets.clear();   //移除所有的元素
        System.out.println(pets.isEmpty());//true
    }
}

Iterator迭代器

迭代器存在的意义:
任何的容器类,都必须有某种方式可以插入元素并将它们再次取回。而对于List来说,add()方法是插入元素的方法之一,而get()方法是取出元素的方法之一。但是如果你此时的容器类是Set,那么取出的方式就不同了,如何保证不管容器的类型,而进行遍历,这时Iterator就派上用场了!

Java中的Iterator只能单向移动,这个Iterator只能用来:

例子:

public class SimpleIteration {
    public static void main(String[] args) {
        String[] arrs = {"Mouse","Dog","Cats","Pug","Manx"};
        List<String> pets = new ArrayList<>();
        pets.addAll(Arrays.asList(arrs));
        
        Iterator<String> it = pets.iterator();
        while(it.hasNext()){
            String p = it.next();
            System.out.print(p + " ");
        }
        System.out.println();
        for(String p : pets){
            System.out.print(p + " ");
        }
        System.out.println();
        it = pets.iterator();
        for(int i=0;i<5;i++){
            it.next();
            it.remove();
        }
        System.out.println(pets);
    }
}

Iterator还可以移除由next()产生的后一个元素,这意味着在调用remove()之前必须先调用next();

使用Iterator编写通用的输出方法
public class CrossContainerIteration {
    public static void display(Iterator<String> it){
        while(it.hasNext()){
            String p = it.next();
            System.out.print(p+" ");
        }
        System.out.println();
    }
    public static void main(String[] args) {
        List<String> pets = new ArrayList<String>();
        String[] arrs = {"Mouse","Dog","Cats","Pug","Manx"};
        pets.addAll(Arrays.asList(arrs));
        LinkedList<String> petsLinked = new LinkedList<String>(pets);
        HashSet<String> hash = new HashSet<>(pets);
        TreeSet<String> tree = new TreeSet<>(pets);
        display(petsLinked.iterator());
        display(hash.iterator());
        display(tree.iterator());
    }
}

这里我们编写了一个display()方法用于遍历,Iterator能够将遍历序列的操作与序列底层的结构分离。

使用ListIterator遍历

ListIterator是一个更加强大的Iterator的子类型,它只能用于各种List类的访问。Iterator只能向前移动而ListIterator可以双向移动,它还可以产生相对于迭代器在列表中指向的当前位置的前一个和后一个元素的索引,并且可以使用set()方法替换它访问过的最后一个元素。

上一篇下一篇

猜你喜欢

热点阅读