码迷,mamicode.com
首页 > 其他好文 > 详细

Collection-Iterator-foreach

时间:2017-07-06 23:46:39      阅读:297      评论:0      收藏:0      [点我收藏+]

标签:void   描述   ==   hit   jdk   retain   sys   nbsp   size   

一.Collection(java.util)
1.概述:具有相同性质的一类事物的汇聚的整体,称为集合.任何集合都包含三块内容:对外的接口/接口的实现/对集合运算的算法.
         java中使用Collection来表示单列集合顶层的接口.
         public interface Collection<E> extends Itretaor<E>{}
         注意:接口不能直接实例化,得需要其子类
2.特点及体系:Collection为顶层接口,仅描述集合的共性内容.常用的子接口为 List 和 Set.
     共性内容:
         (1)集合容器的的长度可以改变;
         (2)存储的是同一个类型的数据;
         (3)只能存引用数据类型(基本数据类型需进行装箱操作);
     List : 有序/允许重复;
     Set : 无序/不允许重复;
     Queue:队列接口;
     SortedSet:对集合中的数据进行排序;
3.常用方法:
     public boolean add(E e){}:向集合中插入对象;
     public boolean addAll(Collection<? extends E> c){}:插入一个集合的内容;
     public void clear(){}:清除此集合中的所有内容,但是保留该集合容器;
     public boolean contains(Object o){}:判定某一对象是否存在该集合中;
     public boolean containsAll(Collection<?> c){}:判断一组对象是否存在该集合中;
     public boolean equals(Object o){}:对象比较;
     public int hashCode(){}:哈希值;
     public boolean isEmpty(){}:判断集合是否为空;
     public Iterator<E> iterator(){}:为Iterator接口实例化;返回迭代器实例化对象;
     public boolean remove(Object o){}:删除指定对象;
     public boolean removeAll(Collection<?> c){}:删除一组对象;
     public boolean retainAll(Collection<?> c){}:保存指定内容;
     public int size(){}:求出集合大小;
     public Object[] toArray(){}:将一个集合变为对象数组;

代码:

  1 import java.util.Collection;
  2 	import java.util.ArrayList;
  3 
  4 	public class Coll{
  5 		public static void main(String[] args){
  6 			//通过子类实例化接口对象,因为接口全为抽象无法直接实例化
  7 			Collection<String> c = new ArrayList<String>();
  8 			//添加元素add()
  9 			c.add("Rose");
 10 			c.add("Jack");
 11 			//查看集合内容
 12 			System.out.println(c);//[Rose, Jack]
 13 			//查看集合长度
 14 			System.out.println(c.size());//2
 15 			//查看是否包含tom
 16 			System.out.println(c.contains("tom"));//false
 17 			//查看是否包含Tom
 18 			c.add("Tom");
 19 			System.out.println(c.contains(new String("Tom")));//true--new String("Tom")返回一个"Tom"字符串
 20 			//删除指定对象remove()
 21 			c.remove("Rose");
 22 			System.out.println(c);//[Jack, Tom]
 23 			//删除所有对象-保留集合
 24 			c.clear();
 25 			System.out.println(c);//[]
 26 			//判断是否为空
 27 			System.out.println(c.isEmpty());//true
 28 		}
 29 	}

二.Iterator(java.util)
1.定义:专门操作集合的工具类,只要碰到集合的输出操作就一定使用Iterator接口
     pbulic interface Iterator<E>
2.常用方法:
     public boolean hasNext(){}:判断是否存在下一个值;
     public E next(){}:取出当前元素;
     public void remove(){}:移除当前元素;
3.注意事项:
(1)使用迭代器只能删除数据,不能添加数据;
(2)迭代器迭代数据的过程中,不能使用集合自带的方法,改变集合的长度!否则会报异常:ConcurrentModificationException 并发修改异常!
(3)注意在构建迭代器的时候,其后<>内指定类型(如:Iterator<String> it = Collection.iterator();),否则运算会出错,仅打印没问题;

  1 代码1://需求:利用Iterator输出集合内容
  2 	import java.util.Collection;
  3 	import java.util.ArrayList;
  4 	import java.util.Iterator;
  5 
  6 	public class IteratorDemo{
  7 		public static void main(String[] args){
  8 			//通过子类实例化对象实例化Collection对象
  9 			Collection<String> c = new ArrayList<String>();
 10 			//向集合c中添加元素
 11 			c.add("Green");
 12 			c.add("Smith");
 13 			c.add("Philip");
 14 			//通过Collection的iterator方法,创建iterator对象
 15 			Iterator<String> it =c.iterator();	//注意在构建迭代器的时候,指定类型(此处为<String>),否则运算会出错,仅打印没问题
 16 			//输出集合c中的所有元素
 17 			while(it.hasNext()){
 18 				System.out.print(it.next()+"\t");//注意,建议判断一次仅调用一次next()方法,由于next()在输出的时候也会使指针向前移动,容易发生"NoSuchElementException"
 19 				//System.out.println("i like"+it.next());//java.util.NoSuchElementException
 20 			}
 21 		}
 22 	}
  1 代码2://需求:删除元素
  2 import java.util.Collection;
  3 import java.util.ArrayList;
  4 import java.util.Iterator;
  5 
  6 public class IteratorDemo{
  7 	public static void main(String[] args){
  8 		//通过子类实例化对象实例化Collection对象
  9 		Collection<String> c = new ArrayList<String>();
 10 		//向集合c中添加元素
 11 		c.add("Green");
 12 		c.add("Smith8");
 13 		c.add("Philip");
 14 		//foreach迭代
 15 		for(String s : c){
 16 			System.out.print(s+"\t")	//Green Smith8 Philip
 17 		}
 18 		System.out.println("====================")
 19 		//通过Collection的iterator方法,创建iterator对象
 20 		Iterator<String> it =c.iterator();//注意在构建迭代器的时候,指定类型(此处为<String>),否则运算会出错,仅打印没问题
 21 		//输出集合c中的所有元素
 22 		while(it.hasNext()){
 23 			//取出集合中的元素
 24 			String str = it.next();
 25 			//判断是否含有数字--正则(字符串最后一位是数字的)
 26 			if(str.matches("\\w+\\d")){
 27 				it.remove();
 28 			}else{
 29 				System.out.print(str+"\t");// Green Philip
 30 			}
 31 		}
 32 		System.out.print("删除带数字的名称后为:"+c+"\t");//删除带数字的名称后为:[Green, Philip]
 33 	}
 34 }
 35 

三.for--each
1.作用:增强for是JDK1.5之后出现的新特性,可以用于迭代(遍历/循环)集合或数组!可以使用增强for替代迭代器,获取集合的元素内容!
2.格式:
     for(数据类型 变量名 : 对象名或数组名){
             集合或数组中的每一个元素!
         }
3.代码:见二.Iterator代码2

Collection-Iterator-foreach

标签:void   描述   ==   hit   jdk   retain   sys   nbsp   size   

原文地址:http://www.cnblogs.com/huguangqin/p/7128696.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!