Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Exemple de la boucle ‘forin’: un type de boucle personnalisé*


{filelink=4412}

import java.io.IOException;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
 
public class ForInTester {
 
  public ForInTester() {
  }
 
  public List getList() {
    List list = new LinkedList();
 
    for (int i = 1; i <= 100; i++) {
      list.add("Item " + i);
    }
 
    return list;
  }
 
  public void testForLoop(PrintStream out) throws IOException {
    List list = getList();
 
    for (Iterator i = list.iterator(); i.hasNext(); ) {
      Object listElement = i.next();
      out.println(listElement.toString());
 
    }
  }
 
  public void testForInLoop(PrintStream out) throws IOException {
    List list = getList();
 
    for (Object listElement : list) {
      out.println(listElement.toString());
 
    }
  }
 
  public void testArrayLooping(PrintStream out) throws IOException {
    int[] primes = new int[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 };
 
    for (int n : primes) {
      out.println(n);
    }
  }
 
  public void testObjectArrayLooping(PrintStream out) throws IOException {
    List[] list_array = new List[3];
 
    list_array[0] = getList();
    list_array[1] = getList();
    list_array[2] = getList();
 
    for (List l : list_array) {
      out.println(l.getClass().getName());
    }
  }
 
  public void determineListPosition(PrintStream out, String[] args)
    throws IOException {
 
    List<String> wordList = new LinkedList<String>();
 
    for (int i=0; i<args.length; i++) {
      wordList.add("word " + (i+1) + ": '" + args[i] + "'");
    }
 
    for (String word : wordList) {
      out.println(word);
    }
 
    StringBuffer longList = new StringBuffer();
    for (int i = 0, len=wordList.size(); i < len; i++) {
      if (i < (len-1)) {
        longList.append(wordList.get(i))
                .append(", ");
      } else {
        longList.append(wordList.get(i));
      }
    }
    out.println(longList);
  }
 
  public void removeListItems(PrintStream out, String[] args)
    throws IOException {
 
    List<String> wordList = new LinkedList<String>();
 
    for (int i=0; i<args.length; i++) {
      wordList.add("word " + (i+1) + ": '" + args[i] + "'");
    }
 
    for (Iterator i = wordList.iterator(); i.hasNext(); ) {
      String word = (String)i.next();
      if (word.indexOf("1") != -1) {
        i.remove();
      }
    }
 
    for (String word : wordList) {
      out.println(word);
    }
  }
 
  public static void main(String[] args) {
    try {
      String[] s = new String[]{"a","b","c"};
 
      ForInTester tester = new ForInTester();
 
      tester.testForLoop(System.out);
      tester.testForInLoop(System.out);
 
      tester.testArrayLooping(System.out);
      tester.testObjectArrayLooping(System.out);
 
      tester.determineListPosition(System.out, s);
      tester.removeListItems(System.out, s);
 
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

A Voir sur le même Sujet:

  • boucle FORin js