Java basics (Collection and List)

1. Overview of collections

Role: Object-oriented languages ​​are all in the form of objects, and collection classes facilitate access to multiple objects.

       Note: Although the collection class and the array belong to the same container, the collection can store objects, but the length is fixed, and the length of the collection can be changed. Arrays can store basic data types, and collections can only store object types (class types).

Two, Collection interface

  • Common methods:

Note: Collection is an interface, you cannot directly create an object, you need to use its subclass object ArrayList

Examples:

        Collection list=new ArrayList();
        //添加元素的方法,返回true或false
        System.out.println(list.add("yyy"));
        System.out.println(list.add("wtc"));
        System.out.println("打印集合"+list);
        //返回集合的长度
        System.out.println(list.size());
        //删除指定元素,返回true或false
        list.remove("yyy");
        //判断是否包含指定元素,返回true或false
        System.out.println(list.contains("wtc"));
        //清空集合
        list.clear();
        //判断集合是否为空,返回true或false
        System.out.println(list.isEmpty());

operation result:

  • Collection batch operation method

Examples:

        Collection list1=new ArrayList();
        list1.add("111");
        list1.add("222");
        Collection list2=new ArrayList();
        list2.add("aaa");
        list2.add("bbb");
        list2.add("111");
        Collection list3=new ArrayList();
        list3.add("aaa");
        //判断list2中是否包含list3中的所有元素
        System.out.println(list2.containsAll(list3));
        //移除交集
        list2.removeAll(list1);
        System.out.println("list2"+list2);
        //只留下交集
        list3.retainAll(list2);
        System.out.println("list3"+list3);
        //把list2中的元素全部添加到list1中
        list1.addAll(list2);
        System.out.println("list1"+list1);

operation result:

  • Collection traversal method
        Collection list=new ArrayList();
        list.add("yyy");
        list.add("wtc");
        //第一种使用数组
        Object[] os=list.toArray();
        for(int i=0;i<os.length;i++)
            System.out.println(os[i]);
        //第二种使用迭代器
        Iterator it =list.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
        //第三种使用增强for
        for(Object o:list)
            System.out.println(o);

operation result:

Three, List interface

Overview: List represents an ordered Collection, that is, it uses a certain insertion order to "maintain the order of elements". You can precisely control the insertion position of each element in the list, or you can access the element according to the index and search for the element.

  • Common operation methods:

Examples:

        List list=new ArrayList();
        list.add("yyy");
        list.add("wtc");
        //list特有的方法
        list.add(0,"abc");
        //获取指定下标的元素
        Object o=list.get(0);
        System.out.println(o);
        //移除指定下标的元素,返回被删除的元素
        list.remove(0);
        //设置指定元素下标的值
        list.set(0,"yw");
        System.out.println(list);

operation result:

  • List traversal method
        List list=new ArrayList();
        list.add("yyy");
        list.add("wtc");
        /*第一种使用迭代器进行遍历*/
        //获取到迭代器
        ListIterator it=list.listIterator();
        System.out.println("向后遍历:");
        while(it.hasNext()){
            System.out.println(it.next());
        }
        System.out.println("向前遍历:");
        while(it.hasPrevious()){
            System.out.println(it.previous());
        }
        /*第二种用增强for*/
        for(Object o:list){
            System.out.println(o);
        }
        /*第三种for循环*/
        for(int i=0;i<list.size();i++){
            System.out.println(list.get(i));
        }

operation result:

  • Iterator modification
        List list=new ArrayList();
        list.add("yyy");
        list.add("wtc");
        ListIterator it=list.listIterator();
        while(it.hasNext()){
            Object o=it.next();
            if(o.equals("yyy"))
                it.add("❤");
                //list.add("❤");运行时会报错
        }
        System.out.println(list);

operation result:

Published 75 original articles · praised 164 · 110,000 views

Guess you like

Origin blog.csdn.net/qq_41679818/article/details/96333833