集合框架

image

java.util.*

collection体系集合

迭代器:


@Test
    void demo1() {
        Collection collection = new ArrayList<>();
        collection.add("sdf");
        collection.add(111);
        System.out.println(collection);
        collection.remove("sdf");
        collection.remove("111111");
        System.out.println(collection);
        for (Object object : collection) {
            System.out.println(object);
        }
        Iterator it = collection.iterator();
        while (it.hasNext()) {
            Object i =  it.next();
            it.remove();
            System.out.println(i);
        }
        System.out.println(collection);
    }



public class TestCollection {

    /**
     * InnerTestCollection
     */
    public static class InnerTestCollection {
        public String Name;
        public Integer Age;

        public InnerTestCollection() {
        }

        public InnerTestCollection(String name, Integer age) {
            super();
            this.Name = name;
            this.Age = age;
        }

        public Integer getAge() {
            return Age;
        }

        public void setAge(Integer age) {
            Age = age;
        }

        public String getName() {
            return Name;
        }

        public void setName(String name) {
            Name = name;
        }
        @Override
        public String toString() {
            return "name:"+Name + ",age:"+Age;
        }

    }

    public static void main(String[] args) {

        Collection collection = new ArrayList();

        InnerTestCollection inner = new InnerTestCollection("dddddd", 12);
        collection.add(inner);
        InnerTestCollection inner2 = new InnerTestCollection("sdfsdfsdf", 12);
        collection.add(inner2);
        collection.add(inner2);
        System.out.println(collection);

        Iterator it = collection.iterator();
        while (it.hasNext()) {
            InnerTestCollection i = (InnerTestCollection) it.next();
            // System.out.println(i.toString());
        }

        collection.remove(inner2);
        collection.remove(inner2);

        System.out.println(collection);

    }

}

list子接口

list

同如下

重写之后删除

右移一位相当于除以2

vector 使用

linkedlist集合

数据结构:双向链表

源码分析:

Last Updated:
Contributors: 刘荣杰