Java中的迭代器遍历集合

一、以集合HashSet为例

HashSet类中常用的实例方法

  1. //boolean add(Object o)将指定元素添加到此集合
  2. //重复的元素算一个
  3. //int size()返回此集合中的元素个数(其基数)
  4. //boolean   contains(Object o)如果此集合中包含指定的元素,则返回true
  5. //boolean  remove(Object o)如果存在,则从该集合中删除指定的元素
  6. //boolean  isEmpty()如果此集合不包含元素,则返回true
  7. //void clear()从此集合中删除所有元素
package com.wangxing.hashset;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

public class HashSetTest2 {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//HashSet类常用的实例方法
		HashSet<Object> hashSet=new HashSet<>();
		//boolean add(Object o)将指定元素添加到此集合
		
		hashSet.add("hello");
		hashSet.add(12234);
		hashSet.add(true);
		hashSet.add(12.33);
		hashSet.add("world");
		hashSet.add("hello");
		
		//int size()返回此集合中的元素个数(其基数)
		System.out.println("size=="+hashSet.size());
		//重复的元素算一个
		hashSet.add("hello");
		System.out.println("size=="+hashSet.size());
		
		//boolean   contains(Object o)如果此集合中包含指定的元素,则返回true
		String contains=(hashSet.contains("helloworld"))?"存在":"不存在";
		System.out.println("contains=="+contains);
		
		//boolean  remove(Object o)如果存在,则从该集合中删除指定的元素
		
		hashSet.remove("world");
		System.out.println("size=="+hashSet.size());
		
		//boolean  isEmpty()如果此集合不包含元素,则返回true
		System.out.println("isEmpty=="+hashSet.isEmpty());
		
		//void clear()从此集合中删除所有元素
		hashSet.clear();
		System.out.println("isEmpty=="+hashSet.isEmpty());

		
	}
}

 输出结果:

size==5
size==5
contains==不存在
size==4
isEmpty==false

二、使用迭代器遍历HashSet集合

              //Iterator<E>   iterator()以正确的顺序返回改列表中的元素的迭代器
              //hasNext()   判断迭代器是否存在下一个元素【如果存在返回值为true】
              //next()---从迭代器中得到下一个元素的值

//Iterator iterator()返回此集合中元素的迭代器

Iterator<Object> iterator= hashSet.iterator();

1.//for循环使用迭代器

	for (Iterator<Object> iterator1=iterator ; iterator1.hasNext();) {
			Object object = (Object) iterator1.next();
			System.out.println("obj1=="+object);
		}

输出结果:

obj1==12234
obj1==hello
obj1==12.33
obj1==true

2.//while循环使用迭代器

	while (iterator.hasNext()) {
			Object object2= (Object) iterator.next();
				System.out.println("obj2=="+object2);
		}

输出结果:

obj2==12234
obj2==hello
obj2==12.33
obj2==true

3.使用迭代器时必须用同一个迭代器不能使用两个不同的

例子://当有两个迭代器时

		      Iterator<Object> iterator= hashSet.iterator();
		      Iterator<Object> iterator2= hashSet.iterator();

如果while循环使用了两个不同的迭代器就会爆错

while (iterator.hasNext()) {
			Object object2= (Object) iterator2.next();
				System.out.println("obj2=="+object2);
		}

//循环条件用的是迭代器

iterator.hasNext()

//获取元素用的是另一个

iterator2.next();

输出结果:

obj2==12234
obj2==hello
obj2==12.33
obj2==true
Exception in thread "main" java.util.NoSuchElementException

可以看到虽然遍历了集合但是while循环没有停止

使用ArrayList集合也是同样

		ArrayList<Object> arrayList=new ArrayList<>(hashSet);
		      Iterator<Object> iterator3=arrayList.iterator();
		      while (iterator3.hasNext()) {
				Object object = (Object) iterator2.next();
				System.out.println("ob3=="+object);
			}

输出结果:

obj2==12234
obj2==hello
obj2==12.33
obj2==true
Exception in thread "main" java.util.NoSuchElementException

为什么使用不同的Iterator接口(迭代器)会报错呢?

		      Iterator<Object> iterator= hashSet.iterator();
		      Iterator<Object> iterator2= hashSet.iterator();
	while (iterator.hasNext()) {
				Object object2= (Object) iterator2.next();
					System.out.println("obj2=="+object2);
		}

输出结果:

obj2==12234
obj2==hello
obj2==12.33
obj2==true
Exception in thread "main" java.util.NoSuchElementException

原因是

//因为iterator.hasNext()失去了控制效果,

//iterator.hasNext()里面一直是有元素的

//所以iterator.hasNext()结果一直是true

//循环会一直进行

//之后运行到获取元素方法iterator2.next()时,获取的是

 Iterator<Object> iterator2= hashSet.iterator();

iterator2中的元素,iterator中的元素没有被获取,也就没有被释放(删除)所以它的iterator.hasNext()方法

一直都是true,循环会一直进行,当把iterator2中的元素获取完后还继续获取就会报错

从输出结果可以看到当元素获取完后iterator2.hasNext()还判断其中有元素但是

,iterator2.next()里已经没有元素了所以会报错

Exception in thread "main" java.util.NoSuchElementException(找不到下一个元素)

双层while循环遍历

		      int i=0;
		      while (iterator.hasNext()) {
					Object object221= (Object) iterator.next();
					  System.out.println("size1=="+hashSet.size());
						System.out.println("obj221=="+object221);
							while (i<2) {
							Object object222=iterator.next();
							System.out.println("size2=="+hashSet.size());
							System.out.println("obere222=="+object222);
							i++;
						}
				}

输出结果

size1==4
obj221==12234

size2==4
obere222==hello
size2==4
obere222==12.33

size1==4
obj221==true

可以看到iterator.next();是一种顺序输出iterator.next();就像指针一样依照顺序依次指向元素

		      int i=0;
		      while (iterator.hasNext()) {
					Object object221= (Object) iterator.next();
					  System.out.println("size1=="+hashSet.size());
						System.out.println("obj221=="+object221);
						
							while (i<2) {
							Object object222=iterator.next();
							Object object223=iterator.next();
							System.out.println("size2=="+hashSet.size());
							System.out.println("obere222=="+object222);
							System.out.println("obere223=="+object223);
							i++;
						}
							
				}

输出结果:

size1==4
obj221==12234

size2==4
obere222==hello

obere223==12.33

Exception in thread "main" java.util.NoSuchElementException

 HashSet<Object> hashSet2=new HashSet<>();
				hashSet2.add(1);
				hashSet2.add(2);
				hashSet2.add(3);
				hashSet2.add(4);
				hashSet2.add(5);
				hashSet2.add(6);
		      
		      Iterator<Object> iterator11= hashSet2.iterator();

		      int i=0;
		      while (iterator11.hasNext()) {
					Object object221= (Object) iterator11.next();
					  System.out.println("size1=="+hashSet2.size());
						System.out.println("obj221元素=="+object221);
						//while (iterator11.hasNext()) {
						while (i<2) {
							
							Object object222=iterator11.next();
							Object object223=iterator11.next();
							
							System.out.println("size2=="+hashSet2.size());
							System.out.println("obere222元素=="+object222);
							
							System.out.println("obere223元素=="+object223);
							System.out.println(i);
							i++;
						}
							
				}

size1==6
obj221元素==1

size2==6
obere222元素==2
obere223元素==3
0
size2==6
obere222元素==4

obere223元素==5
1
size1==6
obj221元素==6

依次指向下一个元素

猜你喜欢

转载自blog.csdn.net/weixin_52192654/article/details/111831570