【Java编程】Foreach对数组、Collection对象、Iterable对象的遍历

1、foreach介绍及其使用范围

foreach语句是java5的新特征之一,在遍历数组、集合、Iterable对象方面,为开发人员提供了极大的方便。foreach语句是for语句特殊情况下的增强版本,简化了编程,提高了代码的可读性和安全性(不用怕数组越界)。foreach语法支持对数组、Collection对象、Iterable对象的遍历。

2、foreach适用于对数组遍历

		String[] array = {"A","B","C","D","E","F","G"};
		for(String str:array){
			System.out.print(str+" ");
		}

3、foreach适用于对Collection对象遍历

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.TreeSet;

public class ForeachDemo {
	public static void main(String[] args) {
		String[] array = {"A","A","B","E","F","G","C","D"};
		Collection list = new ArrayList(Arrays.asList(array));
		for(String str:list){////按照被添加的顺序保存对象
			System.out.print(str+" ");
		}
		System.out.println();
		Collection linkedList = new LinkedList(Arrays.asList(array));
		for(String str:linkedList){//按照被添加的顺序保存对象
			System.out.print(str+" ");//A A B E F G C D 
		}
		System.out.println();
		Collection hashSet = new HashSet(Arrays.asList(array));
		for(String str:hashSet){//无重复,无序,存储顺序并无实际意义
			System.out.print(str+" ");//D E F G A B C 
		}
		System.out.println();
		Collection treeSet = new TreeSet(Arrays.asList(array));
		for(String str:treeSet){//无重复,按照比较结果的升序保存对象
			System.out.print(str+" ");//A B C D E F G 
		}
		System.out.println();
		Collection linkedHashSet = new LinkedHashSet(Arrays.asList(array));
		for(String str:linkedHashSet){//无重复,按照被添加的顺序保存对象
			System.out.print(str+" ");//A B E F G C D 
		}
	}
}
代码优化,利用上转型抽象出公共方法:
public static void forEachCollection(Collection colleciton){
		for(String str:colleciton){////按照被添加的顺序保存对象
			System.out.print(str+" ");
		}
		System.out.println();
	}
测试用例:
public static void listCollection() {
		String[] array = {"A","A","B","E","F","G","C","D"};
		forEachCollection(new ArrayList(Arrays.asList(array)));//A A B E F G C D 
		forEachCollection(new LinkedList(Arrays.asList(array)));//按照被添加的顺序保存对象 //A A B E F G C D 
		forEachCollection(new HashSet(Arrays.asList(array)));//无重复,无序,存储顺序并无实际意义//D E F G A B C 
		forEachCollection(new TreeSet(Arrays.asList(array)));//无重复,按照比较结果的升序保存对象//A B C D E F G 
		forEachCollection(new LinkedHashSet(Arrays.asList(array)));//无重复,按照被添加的顺序保存对象//A B E F G C D 
	}
3.1、利用迭代遍历Collection对象:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.TreeSet;

public class ForeachDemo {
	
	public static void printCollection(Collection colleciton){
		Iterator it = colleciton.iterator();
		while(it.hasNext()){
			System.out.print(it.next()+" ");
		}
		System.out.println();
	}
	
	public static void main(String[] args) {

		String[] array = {"A","A","B","E","F","G","C","D"};
		printCollection(new ArrayList(Arrays.asList(array)));//A A B E F G C D 
		printCollection(new LinkedList(Arrays.asList(array)));//按照被添加的顺序保存对象 //A A B E F G C D 
		printCollection(new HashSet(Arrays.asList(array)));//无重复,无序,存储顺序并无实际意义//D E F G A B C 
		printCollection(new TreeSet(Arrays.asList(array)));//无重复,按照比较结果的升序保存对象//A B C D E F G 
		printCollection(new LinkedHashSet(Arrays.asList(array)));//无重复,按照被添加的顺序保存对象//A B E F G C D 
	}

}

4、foreach适用于对Iterable对象遍历:

package com.andieguo.iterabledemo;

import java.util.Iterator;

public class IteratorSequence {
	private T[] array = null;
	public IteratorSequence(T[] array){
		this.array = array;
	}
	public Iterable iterator(){//Iterable能够用于foreach
		return new Iterable(){
			@Override
			public Iterator iterator() {
				// TODO Auto-generated method stub
				return new Iterator() {
					private int index = 0;
					@Override
					public boolean hasNext() {
						// TODO Auto-generated method stub
						return index < array.length;
					}

					@Override
					public T next() {
						// TODO Auto-generated method stub
						return array[index++];
					}

					@Override
					public void remove() {
						// TODO Auto-generated method stub
						throw new UnsupportedOperationException();
					}
				};
			}};
	}
	public static void main(String[] args) {
		IteratorSequence iteratorSequence = new IteratorSequence(new String[]{"A","B","C"});
		for(String str:iteratorSequence.iterator()){//iteratorSequence.iterator()返回的是一个Iterable实例,支持foreach循环
			System.out.print(str+" ");
		}
	}
}


你可能感兴趣的:(Java,Java编程)