From d824c4d9a209350ac31120941a6c24d209eedced Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Tue, 13 Oct 2026 13:54:58 +0800 Subject: [PATCH] =?UTF-8?q?s-day02--Collection=E6=8E=A5=E5=8F=A3(=E5=8D=95?= =?UTF-8?q?=E5=88=97=E9=9B=86=E5=90=88)=E5=B8=B8=E7=94=A8=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/inmind/collection01/Demo01.java | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 s-day02/src/com/inmind/collection01/Demo01.java diff --git a/s-day02/src/com/inmind/collection01/Demo01.java b/s-day02/src/com/inmind/collection01/Demo01.java new file mode 100644 index 0000000..55cbf42 --- /dev/null +++ b/s-day02/src/com/inmind/collection01/Demo01.java @@ -0,0 +1,59 @@ +package com.inmind.collection01; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; + +/* +Collection接口常用方法 + public boolean add(E e) : 把给定的对象添加到当前集合中 。 + public void clear() :清空集合中所有的元素。 + public boolean remove(E e) : 把给定的对象在当前集合中删除。 + public boolean contains(E e) : 判断当前集合中是否包含给定的对象。 + public boolean isEmpty() : 判断当前集合是否为空。 + public int size() : 返回集合中元素的个数。 + public Object[] toArray() : 把集合中的元素,存储到数组中。 + */ +public class Demo01 { + public static void main(String[] args) { + //创建一个单列集合Collection对象,现在我们只学过ArrayLsit是它的实现类 + Collection collections = new ArrayList<>();//接口的多态 + /* + public boolean add(E e) : 把给定的对象添加到当前集合中 。 + */ + collections.add("张三"); + collections.add("李四"); + collections.add("王五"); + System.out.println(collections);//底层重写了toStrinng方法 + + /* + public void clear() :清空集合中所有的元素。 + */ + /*collections.clear(); + System.out.println(collections);*/ + /* + public boolean remove(E e) : 把给定的对象在当前集合中删除。 + */ + System.out.println(collections.remove("赵六"));//false + System.out.println(collections.remove("李四"));//true + System.out.println(collections); + /* + public boolean contains(E e) : 判断当前集合中是否包含给定的对象。 + */ + System.out.println(collections.contains("李四"));//false + System.out.println(collections.contains("王五"));//true + + /* + public boolean isEmpty() : 判断当前集合是否为空。 + */ + System.out.println(collections.isEmpty());//false + + /* + public Object[] toArray() : 把集合中的元素,存储到数组中。 + */ + Object[] array = collections.toArray(); + System.out.println(Arrays.toString(array)); + + System.out.println(collections.size());//2 + } +}