Java教程

Java自学-集合(2)

本文主要是介绍Java自学-集合(2),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Java自学-集合(2)

1、集合体系结构

image-20210813064002586

image-20210813064201661

2、Collection集合概述和使用

image-20210813064435230

package lesson01;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/13 6:46
 * FileName: CollectionDemo01
 * Description:
 */
public class CollectionDemo01 {
    public static void main(String[] args) {
        Collection<String> c=new ArrayList<String>();

        c.add("hello");
        c.add("world");
        c.add("java");

        System.out.println(c);
    }
}

3、Collection集合常用方法

image-20210813213946932

鼠标中键或者选中想要查看的类,Ctrl+B。

Alt+7:打开一个窗口,可以看到类的所有信息。

package lesson01;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/13 21:41
 * FileName: CollectionDemo02
 * Description:
 */
public class CollectionDemo02 {
    public static void main(String[] args) {
        Collection<String> c=new ArrayList<String>();

//        System.out.println(c.add("hello"));
//        System.out.println(c.add("world!"));
//        System.out.println(c.add("world!"));
        c.add("hello");
        c.add("world");
        c.add("java");


//        System.out.println(c.remove("world"));
//        System.out.println(c.remove("javaee"));
//        c.clear();
//        System.out.println(c.contains("world"));
//        System.out.println(c.contains("javaee"));

//        System.out.println(c.isEmpty());

        System.out.println(c.size());

        System.out.println(c);
    }
}

4、Collection集合的遍历

image-20210813215415561

package lesson01;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

/**
 * Author: Gu Jiakai
 * Date: 2021/8/13 21:55
 * FileName: IteratorDemo
 * Description:
 */
public class IteratorDemo {
    public static void main(String[] args) {
        Collection<String> c=new ArrayList<String>();

        c.add("hello");
        c.add("world");
        c.add("java");

        Iterator<String> iterator = c.iterator();

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

5、集合使用步骤图解

image-20210813220408816

image-20210813220507471

这篇关于Java自学-集合(2)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!