一.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的更多相关文章

  1. 09 Collection,Iterator,List,listIterator,Vector,ArrayList,LinkedList,泛型,增强for,可变参数,HashSet,LinkedHashSet,TreeSet

    09 Collection,Iterator,List,listIterator,Vector,ArrayList,LinkedList,泛型,增强for,可变参数,HashSet,LinkedHas ...

  2. <JAVA8新增内容>关于集合的操作(Collection/Iterator/Stream)

    因为下文频繁使用lambda表达式,关于Java中的lambda表达式内容请见: http://www.cnblogs.com/guguli/p/4394676.html 一.使用增强的Iterato ...

  3. java 测试:iterator foreach for 三种迭代方式哪种更快?

    代码: public class main { public static void main(String[] p_args){ ArrayList<String> _l_string ...

  4. Collection Iterator 迭代器

    Collection c=new ArrayList(); c.add(123); //迭代器遍历集合 Iterator i=c.Iterator(); while(i.hasNext()) { Sy ...

  5. Iterator,foreach遍历小计

    此博客对同一操作对比两种遍历方式,以个人忘记时快速捡起为目的. 数据表: 三个List: List<Menu> menuList=menuService.getAllMenus(query ...

  6. Scala - Tips

    1- 运行scala命令,提示报错 问题现象: 在Windows7系统中安装scala后(直接安装MSI包,或者解压zip包添加环境变量的方式),执行scala命令报错,但可以执行scala -ver ...

  7. JavaSE中Collection集合框架学习笔记(3)——遍历对象的Iterator和收集对象后的排序

    前言:暑期应该开始了,因为小区对面的小学这两天早上都没有像以往那样一到七八点钟就人声喧闹.车水马龙. 前两篇文章介绍了Collection框架的主要接口和常用类,例如List.Set.Queue,和A ...

  8. Iterator && Iterable Collection && Map

    Java集合类库将集合的接口与实现分离.同样的接口,可以有不同的实现. Java集合类的基本接口是Collection接口.而Collection接口必须实现Iterable接口. 以下图表示集合框架 ...

  9. java 数据结构(八):Iterator接口与foreach循环

    1.遍历Collection的两种方式:① 使用迭代器Iterator ② foreach循环(或增强for循环)2.java.utils包下定义的迭代器接口:Iterator2.1说明:Iterat ...

  10. Collection集合之六大接口(Collection、Set、List、Map、Iterator和Comparable)

    首先,我们先看一下Collection集合的基本结构: 1.Collection接口 Collection是最基本集合接口,它定义了一组允许重复的对象.Collection接口派生了两个子接口Set和 ...

随机推荐

  1. WPF error: does not contain a static 'Main' method suitable for an entry point

    WPF error: does not contain a static &apos;Main&apos; method suitable for an entry point doe ...

  2. iOS :undefined symbols for architecture x86_64

    转自:http://www.th7.cn/Program/IOS/201408/268371.shtml 问题描述:为了适配iPhone 5s的64位处理器,在编译选项中加入了arm64架构.但是发现 ...

  3. AD 学习

    http://blog.csdn.net/lingpaoershiyishiji/article/details/9139527

  4. .NET Framework 2.0安装问题

    在.NET Framework 2.0安装的时候,如果提示 system.deployment.dll失败,另一个程序正在使用此文件,进程无法访问.这种情况下,我们可能的解决方案是: 关闭掉杀毒软件在 ...

  5. error: field has incomplete type

    在头文件使用某一自定义的类的指针或引用时,只需要前置声明该类即可,然而如果该类中有静态成员时,必须包含该类的头文件,而不是使用前置声明.

  6. nodebrew的安装与使用

    创建: 2019/05/10  安装 brew install nodebrew 初始化 nodebrew setup ~/.bash_profile 里添加 export PATH=/usr/loc ...

  7. linux下安装QQ等EXE文件

    安装好linux系统后,发现linux很多自带的软件用起来很不习惯,于是去网上下载QQ等在Windows下经常使用的exe文件. 发现这些文件无法安装,在网上找了一些解决办法,须下载wine,然后才可 ...

  8. Bzoj 3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一

    3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 64  Solved ...

  9. springboot打成jar后获取classpath下文件失败

    原文链接:https://blog.csdn.net/qq_18748427/article/details/78606432 springboot打成jar后获取classpath下文件失败 使用如 ...

  10. 字符串前面u,r,b

    u :代表是对字符串进行unicode编码. 一般英文字符在使用各种编码下, 基本都可以正常解析, 所以一般不带u:py3当对字符串进行操作的时候,默认使用Unicode编码 r/R:非转义的原始字符 ...