Java Iterator, ListIterator 和 foreach语句使用
foreach语句结构:
for(part1:part2){part3};
part2 中是一个数组对象,或者是带有泛性的集合.
part1 定义了一个局部变量,这个局部变量的类型与part2中的对象元素的类型是一致的.
part3 当然还是循环体.
foreach语句是java5的新特征之一,在遍历数组、集合方面,foreach为开发人员提供了极大的方便。 foreach语句是for语句的特殊简化版本,但是foreach语句并不能完全取代for语句,然而,任何的foreach语句都可以改写为for语句版本。 foreach并不是一个关键字,习惯上将这种特殊的for语句格式称之为“foreach”语句。从英文字面意思理解foreach也就是“for 每一个”的意思。实际上也就是这个意思。
package com.twzheng; import java.util.Iterator;
import java.util.ListIterator;
import java.util.LinkedList;
import java.util.Scanner; public class MainClass { private static Scanner scanner;
private static String enterString; /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
scanner = new Scanner(System.in); LinkedList<String> myLinkedList = new LinkedList<String>(); printList(myLinkedList); initialLinkedList(myLinkedList); System.out.println("findMember -> Please enter member value: ");
enterString = scanner.next(); if(findMember(myLinkedList, enterString)){
System.out.println(enterString + " is in the linkedList.");
}else{
System.out.println(enterString + " is not found in the linkedList.");
} printList(myLinkedList);
}
// Java Iterator 使用
private static void printList(LinkedList<String> linkedList){
System.out.println("======================================"); Iterator<String> itr = linkedList.iterator();
// Iterator .hasNext()判断是否还有下一个
if (itr.hasNext()){
System.out.println("Pint linkedList members:");
}else{
System.out.println("The linkedList is empty!");
} int i = 0; while(itr.hasNext()){
// Iterator .next()取得下一个
System.out.println(" <" + i + ">" + itr.next());
i++;
} System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
}
// Java foreach语句使用
private static boolean findMember(LinkedList<String> linkedList, String lValue){
for(String checkedValue: linkedList){
if(checkedValue.equals(lValue)){
System.out.println("Found " + lValue + " at linkedList<" + linkedList.indexOf(checkedValue) + ">.");
return true;
}
}
return false;
}
// Java ListIterator 使用 .next() .previous() .add()
private static boolean addMember(LinkedList<String> linkedList, String lValue){
ListIterator<String> ltr = linkedList.listIterator(); while(ltr.hasNext()){
int comp = ltr.next().compareTo(lValue); if(comp == 0){
// equal, do not add
System.out.println(lValue + " is already inclued in the linkedList.");
return false;
} else if(comp > 0){
// new member
ltr.previous();
ltr.add(lValue);
System.out.println(lValue + " has been added in the linkedList.");
return true;
} else {
// ???
}
} ltr.add(lValue);
return true;
} private static void initialLinkedList(LinkedList<String> linkedList){ System.out.println("Initialize linkedList:"); int choseNumber;
boolean stopEnter = false; try {
do {
System.out.println(" 0 - stop enter ");
System.out.println(" 1 - enter a new member ");
System.out.println("Please chose number: ");
choseNumber = scanner.nextInt(); switch (choseNumber) {
case 0:
stopEnter = true;
break;
case 1:
System.out.println("Please enter new member value: ");
addMember(linkedList, scanner.next());
break;
default:
System.out.println("Enter error!");
break;
}
} while (!stopEnter);
} catch (Exception e) {
// TODO: handle exception
System.out.println("Enter error!");
}
} }