黑马程序员——Java基础--集合(二)

-----------android培训java培训、java学习型技术博客、期待与您交流!------------

第二讲 Collection

一、概述

Collection是层次结构中的根接口。Collection 表示一组对象,这些对象也称为 collection 的元素。一些 collection 允许有重复的元素,而另一些则不允许。一些 collection 是有序的,而另一些则是无序的。JDK 不提供此接口的任何直接 实现:它提供更具体的子接口(如 Set 和 List)实现。

二、Collection功能及演示

1、常见功能

package cn.itheima.collection;

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

/*
 * Collection的功能概述:
 			boolean add(E e):				添加元素
			boolean remove(Object o):		删除元素
			void clear():					删除所有的元素
			boolean contains(Object o):		判断是否包含
			boolean isEmpty():				判断是否为空
			int size():						获取长度
 */
public class CollectionDemo {
	public static void main(String[] args) {
		Collection c = new ArrayList();//创建对象
		c.add("hello");//添加元素
		c.add("world");
		c.add("java");
		System.out.println(c);
		System.out.println("----------------");
		c.remove("world");//删除元素
		System.out.println(c);
		System.out.println("----------------");
		c.clear();//删除所有的元素
		System.out.println(c);
		System.out.println("----------------");
		System.out.println(c.contains("world"));//判断是否包含
		System.out.println("----------------");
		System.out.println(c.isEmpty());//判断是否为空
		System.out.println("----------------");
		System.out.println(c.size());//获取长度
		System.out.println("----------------");
	}
}

结果输出:

[hello, world, java]
----------------
[hello, java]
----------------
[]
----------------
false
----------------
true
----------------
0
----------------

2、常见功能

package cn.itheima.collection;

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

/*
 * Collection的功能概述:
 *  boolean addAll(Collection c):		添加一个集合的元素
	boolean removeAll(Collection c):	移除一个集合的元素,移除一个以上算是移除还是移除所有的才算移除?
	boolean containsAll(Collection c):	判断集合中的否包含一个集合的元素. 包含一个以上算是包含,还是包含所有算是包含?
	boolean retainAll(Collection c):	获取交集. 获取到的交集的元素去哪里了,返回的boolean类型的值表示的是神马意思? 
 */
public class CollectionDemo2 {
	public static void main(String[] args) {
		Collection c = new ArrayList();//创建对象
		//添加元素
		c.add("hello");
		c.add("world");
		c.add("java");
		Collection c1 = new ArrayList();//创建对象
		c1.add("hello");
		c1.add("world");
		System.out.println(c.addAll(c1));//添加一个集合的元素,返回布尔型
		System.out.println(c.removeAll(c1));//移除一个以上算是移除,返回布尔型
		System.out.println(c.containsAll(c1));//包含所有算是包含,返回布尔型
		System.out.println(c.retainAll(c1));//交集的元素到第一个集合里了,返回布尔型
	}
}


结果输出为:

true
true
false
true
三、实例

存储3个手机对象到Collection集合中,并以把集合转换成数组后遍历和迭代器进行遍历

Phone类

package cn.itheima.collection;
//定义一个手机类
public class Phone {
	//成员变量
	private String type;
	private int price;
	private String color;
	//构造方法
	public Phone() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Phone(String type, int price, String color) {
		super();
		this.type = type;
		this.price = price;
		this.color = color;
	}
	//成员方法
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	
}

CollectionTest类

package cn.itheima.collection;

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

/*
 * 编写程序,存储3个手机对象到Collection集合中
	把集合转换成数组后遍历
	使用迭代器进行遍历
	分析:
	1、定义一个手机类
	2、创建集合对象
	3、创建手机对象
	4、添加到集合中
	5、集合转换成数组遍历
	6、用迭代器进行遍历
 */
public class CollectionTest {
	public static void main(String[] args) {
		//创建集合对象
		Collection c = new ArrayList();
		//创建手机对象
		Phone p1 = new Phone("酷派",1999,"土豪金");
		Phone p2 = new Phone("苹果",4999,"土豪金");
		Phone p3 = new Phone("小米",1799,"中国红");
		//添加到集合中
		c.add(p1);
		c.add(p2);
		c.add(p3);
		printArray1(c);
		System.out.println("-----------------");
		printArray2(c);	
	}
	public static void printArray1(Collection c){
		//集合转换成数组
		//定义一个数组
		Object[] arr = c.toArray();
		for (int i = 0; i < arr.length; i++) {
			Phone p = (Phone)arr[i];
			System.out.println(p.getType()+"-----"+p.getPrice()+"-----"+p.getColor());
		}
	}
	public static void printArray2(Collection c){
		//用迭代器进行遍历
		Iterator it = c.iterator();
		while(it.hasNext()){
			Phone p = (Phone)it.next();
			System.out.println(p.getType()+"-----"+p.getPrice()+"-----"+p.getColor());
		}
	}
}


结果输出为:

酷派-----1999-----土豪金
苹果-----4999-----土豪金
小米-----1799-----中国红
-----------------
酷派-----1999-----土豪金
苹果-----4999-----土豪金
小米-----1799-----中国红

-----------android培训java培训、java学习型技术博客、期待与您交流!------------

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值