60 lines
2.2 KiB
Java
60 lines
2.2 KiB
Java
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<String> 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
|
||
}
|
||
}
|