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!"); } } }