其他分享
首页 > 其他分享> > 对象持有-容器

对象持有-容器

作者:互联网

一、容器

1. 集合

2. 常见方法

    private static void method01() {
        /*Arrays:
         * 1. 参数为一个可变参数
         * 2. 返回值:名字为ArrayList的类,是 Arrays的静态内部类
         * 3. 得到的list不能add或者remove,因为内部类中没有实现,
         *    UnsupportedOperationException*/
        List<String> stringList = Arrays.asList("FIRST", "SECOND", "THIRD");
        List<Integer> integerList = Arrays.asList(new Integer[]{1, 23, 43});
        System.out.println(stringList);
    }

    private static void method02() {
        /*Collections.addAll()
         * 1. 参数1: 集合, 参数2: 可变参数
         * 2. 返回值为是否添加成功, 只要成功添加一个元素,结果就为true
         * 3. 向参数1中的集合,添加一定的元素*/
        Collection<String> stringList = new HashSet<>();
        stringList.add("FIRST");
        stringList.add("SECOND");
        boolean b = Collections.addAll(stringList, "FOURTH", "FIVE", "FIRST");
        System.out.println(b);
        System.out.println(stringList);
    }

    private static void method03() {
        Collection<String> stringList = new HashSet<>();
        stringList.add("FIRST");
        stringList.add("SECOND");

        Collection<String> secondList = new HashSet<>();
        stringList.add("first");
        stringList.add("second");
        /* Collection.addAll()
        *  1. 合并两个集合*/
        boolean b = stringList.addAll(secondList);
    }

3. List

4. 迭代器

/*不关心容器的类型,而只关心容器内存储的数据
     * 迭代器统一了对容器的访问方式*/
    private static <E> void method01(Collection<E> collection) {
        /*获取到一个迭代器*/
        Iterator iterator = collection.iterator();

        while (iterator.hasNext()) {
            E next = (E) iterator.next();
            System.out.println(next);
            /*将集合中的元素移除*/
            iterator.remove();
        }

        System.out.println(collection);
    }

    /*ListIterator: 继承Iterator,可以两边进行遍历*/
    private static <E> void method02(List<E> list) {
        ListIterator<E> iterator = list.listIterator();

        while (iterator.hasNext()) {
            E next = iterator.next();
            System.out.println(next);
        }

        while (iterator.hasPrevious()) {
            E previous = iterator.previous();
            System.out.println(previous);
        }
    }

5. Stack

package com.erick;

import java.util.Stack;

/*用栈实现队列
 * 1. 用两个栈来实现一个队列
 * 2. 输入栈,输出栈,栈:可以用jdk中提供的stack类*/
public class Demo03 {

}

class ErickQueue<E> {

    private Stack<E> inStack = new Stack<>();
    private Stack<E> outStack = new Stack<>();

    public ErickQueue() {
    }

    public void push(E e) {
        inStack.push(e);
    }

    public E pop(E e) {
        if (outStack.isEmpty()) {
            fromInToOut();
        }
        return outStack.pop();
    }

    /*peek: 返回队列开头的元素*/
    public E peek() {
        if (outStack.isEmpty()) {
            fromInToOut();
        }
        return outStack.peek();
    }

    /*empty:  判断队列是否为空*/
    public Boolean empty() {
        if (inStack.isEmpty() && outStack.isEmpty()) {
            return true;
        }

        return false;
    }

    private void fromInToOut() {
        for (E e : inStack) {
            outStack.push(e);
        }
    }

}

6. Set

7. Queue

标签:容器,iterator,对象,void,outStack,private,持有,add,stringList
来源: https://blog.csdn.net/weixin_43374578/article/details/123029441