从零双排学java之集合collection

                集合collection

集合跟数组一样都是一种容器 

 

集合为什么会出现?

数组有什么弊端:

 1.长度一旦确定不能修改

 2.只能保存同一种类型的元素

 集合的好处:

 * 1.长度可变

 * 2.可以存放不同类型的元素

 * 注意:集合只能存  对象   也就是引用类型(基本类型会自动装箱成包装类)


// 获取元素个数
		System.out.println(collection.size());
// 判断包含
		boolean rel1 = collection.contains("a");
		System.out.println(rel1);
//removeAll
		//清除c1中  c2集合中的所有元素
		//有相同的元素就返回true  没有就返回false
		c1.removeAll(c2);

// 删除元素
		boolean rel2 = collection.remove("m");
		System.out.println(rel2);

// 判断是不是空的集合
		boolean rel3 = collection.isEmpty();
		System.out.println(rel3);

// 集合转数组
		Object[] array = collection.toArray();
		for (Object object : array) {
			System.out.println(object);
		}

// 清空数组
		collection.clear();
		System.out.println(collection);
//removeAll
		//清除c1中  c2集合中的所有元素
		//有相同的元素就返回true  没有就返回false
		c1.removeAll(c2);

//retainAll  返回的是两个集合的交集
		//返回值看调用者是否发生变化 发生变化true
		boolean rel =c1.retainAll(c2);




猜你喜欢

转载自blog.csdn.net/jsyMax/article/details/80368880