JAVA 中集合的基本功能

* 面试题:
 *         数组和集合的区别?
 * 1)长度的区别:
 *         数组:长度是固定的
 *         集合:长度是可变的
 * 2)存储类型的区别
 *         数组:可以存储基本类型、也可以存储引用类型
 *         集合:只能存储引用类型
 * 3)存储元素的区别
 *         数组:存储元素类型保持一致
 *         集合:可以存储不同引用类型的元素
 * 
 * Collection:一些 collection 允许有重复的元素,而另一些则不允许。
 * 一些 collection 是有序的,
 * 而另一些则是无序的。JDK 不提供此接口的任何直接 
 * 实现:它提供更具体的子接口(如 Set 和 List)实现
 * 
 * 
 * Collection的基本功能:
 *         boolean add(Object e):添加任意的引用类型元素
 *         boolean remove(Object o):删除指定元素
 *         void clear():删除集合中的所有元素
 * 
 *         boolean contains(Object o):判断是否包含指定的元素
 *         boolean isEmpty():判断集合是否为空
 * 
 *         int size():获取集合中的元素数量
 *         Object[] toArray():将集合进行遍历-->将集合转换成数组遍历(传统方式)
 *         Iterator<Object> iterator():获取Collection集合的迭代器(集合的专有遍历方式)

@SuppressWarnings("all")
public class CollectionDemo {
	public static void main(String[] args) {
		Collection c=new ArrayList();
		System.out.println(c);//集合重写了Object类中toString()方法
//		boolean add(Object e):添加任意的引用类型元素
		c.add("hello");
		c.add("dsa");
//		
		System.out.println(c);
		System.out.println("-------------------");
//		boolean remove(Object o):删除指定元素
//		System.out.println(c.remove("dsa"));
//		System.out.println(c);
//		System.out.println("-------------------");
//		void clear():删除集合中的所有元素
		c.clear();
		System.out.println(c);
		System.out.println("-------------------");
//		boolean contains(Object o):判断是否包含指定的元素
		System.out.println(c.contains("hello"));
		System.out.println(c);
		System.out.println("------------------");
		System.out.println(c.isEmpty());
		System.out.println("------------------");
		//集合中获取长度:size()
		//数组中获取长度:length属性
		//字符串中获取长度:length()方法
		System.out.println(c.size());
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值