Java集合综述

1. 集合类和数组

(1)数组元素可以是基本数据类型也可以是对象,而集合里只能保存对象。

(2)数组创建之后长度是固定的不能改变,集合类创建后长度可以随着数据量的大小自动增长。

2. Java集合接口由两个接口派生:Collection和Map。

3.Collection接口是List、Set、Queue接口的父接口,常用方法如下:

Methods  
修饰符与类型方法与描述
booleanadd(E e)
Ensures that this collection contains the specified element (optional operation).
booleanaddAll(Collection<? extendsE> c)
Adds all of the elements in the specified collection to this collection (optional operation).
voidclear()
Removes all of the elements from this collection (optional operation).
booleancontains(Object o)
Returns true if this collection contains the specified element.
booleancontainsAll(Collection<?> c)
Returns true if this collection contains all of the elements in the specified collection.
booleanequals(Object o)
Compares the specified object with this collection for equality.
inthashCode()
Returns the hash code value for this collection.
booleanisEmpty()
Returns true if this collection contains no elements.
Iterator<E>iterator()
Returns an iterator over the elements in this collection.
booleanremove(Object o)
Removes a single instance of the specified element from this collection, if it is present (optional operation).
booleanremoveAll(Collection<?> c)
Removes all of this collection's elements that are also contained in the specified collection (optional operation).
booleanretainAll(Collection<?> c)
Retains only the elements in this collection that are contained in the specified collection (optional operation).
intsize()
Returns the number of elements in this collection.
Object[]toArray()
Returns an array containing all of the elements in this collection.
<T> T[]toArray(T[] a)
Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array
4.Collection遍历的两种方式:Iterator和foreach

Collection<String> col = new HashSet<String>();
		col.add("a");
		col.add("b");
		col.add("c");
		col.add("d");
		
		//Iterator遍历
		Iterator it = col.iterator();
		while(it.hasNext())
		{
			System.out.print(it.next());
		}
		System.out.println();
		
		//foreach 遍历
		for(String str:col)
		{
		System.out.print(str);	
		}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值