package Exer_11;
import java.util.Iterator;
import java.util.Vector;
/**
* @author lzcysd
*
*/
public class InteratorTest {
/**
* @param args
*/
public static void main(String[] args) {
String[] num = {"one","two","three","four","five","six","seven","eight","nine","ten"};
Vector<String> aVector = new Vector<String>(java.util.Arrays.asList(num));
//Vector是同步的,线程安全。ArrayList是非同步的,效率高
System.out.println("Before Vector: " + aVector);
Iterator <String> nums = aVector.iterator();
//iterator方法将返回一个Iterator
while(nums.hasNext()){ //检查序列中是否还有元素
String aString = nums.next(); //next获得序列中是否还有元素
System.out.println(aString);
if(aString.length() > 4) //去除大于4个单词的词语
nums.remove();
//remove()将去除集合(不是Interator类)中最后调用next()返回的元素
}
System.out.println("After Vector: " + aVector);
}
}
/* Before Vector: [one, two, three, four, five, six, seven, eight, nine, ten]
* one
* two
* three
* four
* five
* six
* seven
* eight
* nine
* ten
* After Vector: [one, two, four, five, six, nine, ten]
*/
----------------------------------------------------------------------------------------------------------------------------------
package Exer_11;
/*
* 写一个方法,使用Iterator遍历Collection,并打印容器中每个对象的toString()
* 填充各种类型的Collection,然后对其使用此方法
*/
import java.util.*;
import static java.lang.System.out;
public class Exer_11_11 {
public static void printAny(Collection c){
Iterator it = c.iterator();
while(it.hasNext())
out.print(it.next() + "");
out.println();
}
public static void main(String[] args){
ArrayList<Integer> al =
new ArrayList<Integer>(Arrays.asList(1, 2, 3 ));
LinkedList<Character> ll =
new LinkedList<Character>(Arrays.asList('a', 'b', 'c'));
HashSet<Float> hs =
new HashSet<Float>(Arrays.asList(1.1f, 2.2f, 3.3f));
TreeSet<Double> ts =
new TreeSet<Double>(Arrays.asList(1.11, 2.22, 3.33));
LinkedHashSet<Integer> lhs =
new LinkedHashSet<Integer>(Arrays.asList(11, 22, 33));
printAny(al);
printAny(ll);
printAny(hs);
printAny(ts);
printAny(lhs);
}
}
/*
* 123
* abc
* 1.12.23.3
* 1.112.223.33
* 112233
*/
-----------------------------------------------------------------------------------------------------------------------------------
package Exer_11;
import java.util.*;
/**
* ListIterator是一个更强大的Iterator的子类型,他只能用于List类
* 可以双向移动,可以使用set()方法替换它访问过的最后一个元素
* 还可以调用listIterator(n)方法创建一个一开始就指向索引为n的元素处ListIterator
* @author lzcysd
*
*/
public class ListIteratiorTest {
/**
* @param args
*/
public static void main(String[] args) {
List<String> aList = new ArrayList<String>(Arrays.asList("aaa","bbb","ccc","ddd","eee","fff","ggg","hhh"));
ListIterator<String> it = aList.listIterator();
while(it.hasNext())
System.out.print(it.next() + ", " + it.nextIndex() +
", " + it.previousIndex() + ";" );
System.out.println();
while(it.hasPrevious())
System.out.print(it.previous() + " ");
System.out.println();
System.out.println(aList);
it = aList.listIterator(3); //it的第一个元素为aList第4个元素
while(it.hasNext()){
String stemp =it.next();
it.set("3" + stemp);
//相当于把aList从第4个元素(索引为3)开始的其他元素的内容前面都加上3
}
System.out.println(aList);
}
}
/** output
* aaa, 1, 0;bbb, 2, 1;ccc, 3, 2;ddd, 4, 3;eee, 5, 4;fff, 6, 5;ggg, 7, 6;hhh, 8, 7;
* hhh ggg fff eee ddd ccc bbb aaa
* [aaa, bbb, ccc, ddd, eee, fff, ggg, hhh]
* [aaa, bbb, ccc, 3ddd, 3eee, 3fff, 3ggg, 3hhh]
*/