JAVA高级基础(2)---Collection

Collection

Collection接口成员方法

        boolean add(E e)
        boolean remove(Object o)
        void clear()
        boolean contains(Object o)
        boolean isEmpty()
        int size()
        **************************************************
        boolean addAll(Collection c)
        boolean removeAll(Collection c)
        boolean containsAll(Collection c)
        boolean retainAll(Collection c)
        **************************************************
        Object[] toArray()
        把集合转成数组,可以实现集合的遍历
        Iterator iterator()
        迭代器,集合的专用遍历方式

注:更多详细方法请自行在 API 上查找

package org.lanqiao.collection.demo;

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

public class CollectionTest {
	
	public static void main(String[] args) {
		//创建集合:Collection
		Collection c = new ArrayList();
		//添加元素
		boolean b= c.add("aa");
		c.add(1);
		c.add(true);
		c.add('n');
		/*
		//判断集合中是否包含某一个元素
		boolean flag = c.contains(1);
		System.out.println(flag);
		//移除集合中的元素
		c.remove(true);
		//判断集合是否为空
		System.out.println(c.isEmpty());
		//清空集合
		c.clear();
		//可以将集合转为一个数组
		Object[] cArr = c.toArray();
		
		//输出集合中的元素
		//size()获取集合中元素的个数
		for(int i = 0 ; i < cArr.length;i++) {
			System.out.println(cArr[i]);
		}
		System.out.println(c.isEmpty());*/
		/*
		 * 所有的以All结尾的方法
		 * boolean addAll(Collection c)
			boolean removeAll(Collection c)
			boolean containsAll(Collection c)//判断此集合是否包含指定集合中的所有的元素
			
			 * 判断此集合是否和指定的集合存在交集
			 * 如果存在交集 并将交集保存在此集合中  
			 * 如果此集合发生了改变 ,则返回true  否则返回false
			 * 
			 
			boolean retainAll(Collection c)
		 */
		Collection c2 = new ArrayList();
		c2.add("www");
		c2.add("yyyy");
		c2.add("aa");
		//c.addAll(c2);
		Object[] arr = c.toArray();
		for(int i = 0 ; i < arr.length;i++) {
			System.out.println(arr[i]);
			
		}
		//c.removeAll(c2);
		System.out.println("-------------");
		Object[] arr1 = c.toArray();
		for(int i = 0 ; i < arr1.length;i++) {
			System.out.println(arr1[i]);
			
		}
		System.out.println("-------------");
		boolean f = c.containsAll(c2);
		System.out.println(f);
		System.out.println("-------------");
		boolean bb = c.retainAll(c2);
		System.out.println(bb);
		Object[] arr3 = c.toArray();
		for(int i = 0 ; i < arr3.length;i++) {
			System.out.println(arr3[i]);
			
		}
		
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值