Java for In(forin) Tester : Foreach « Language Basics « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Language Basics » Foreach 




Java for In(forin) Tester
Java for In(forin)  Tester


/*
License for Java 1.5 'Tiger': A Developer's Notebook
     (O'Reilly) example package

Java 1.5 'Tiger': A Developer's Notebook (O'Reilly) 
by Brett McLaughlin and David Flanagan.
ISBN: 0-596-00738-8

You can use the examples and the source code any way you want, but
please include a reference to where it comes from if you use it in
your own products or services. Also note that this software is
provided by the author "as is", with no expressed or implied warranties. 
In no event shall the author be liable for any direct or indirect
damages arising in any way out of the use of this software.
*/


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;
  }

  /**
   * <p>Test a very basic, pre-Java 1.5 for loop</p>
   */
  public void testForLoop(PrintStream outthrows IOException {
    List list = getList()// initialize this list elsewhere

    for (Iterator i = list.iterator(); i.hasNext()) {
      Object listElement = i.next();
      out.println(listElement.toString());

      // Do something else with this list object
    }
  }

  /**
   * <p>Test a very basic, Java 1.5 for/in loop</p>
   */
  public void testForInLoop(PrintStream outthrows IOException {
    List list = getList()// initialize this list elsewhere

    for (Object listElement : list) {
      out.println(listElement.toString());

      // Do something else with this list object
    }
  }

  /**
   * <p>Test a simple array iteration</p>
   */
  public void testArrayLooping(PrintStream outthrows IOException {
    int[] primes = new int[] { 2357111317192329 };
    
    // Print the primes out using a for/in loop
    for (int n : primes) {
      out.println(n);
    }
  }

  /**
   * <p>Test an object array iteration</p>
   */
  public void testObjectArrayLooping(PrintStream outthrows 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());
    }
  }

  /**
   * <p>Show list position in a loop (not possible with for/in)</p>
   */
  public void determineListPosition(PrintStream out, String[] args
    throws IOException {

    List<String> wordList = new LinkedList<String>();

    // Impossible to assign the words, since the iterator is used
    for (int i=0; i<args.length; i++) {
      wordList.add("word " (i+1": '" + args[i"'");
    }

    // You can print the words using for/in, but not assign them
    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);
  }

  /**
   * <p>for/in can't remove items using an Iterator</p>
   */
  public void removeListItems(PrintStream out, String[] args
    throws IOException {

    List<String> wordList = new LinkedList<String>();

    // Assign some words
    for (int i=0; i<args.length; i++) {
      wordList.add("word " (i+1": '" + args[i"'");
    }

    // Remove all words with "1" in them. Impossible with for/in
    for (Iterator i = wordList.iterator(); i.hasNext()) {
      String word = (String)i.next();
      if (word.indexOf("1"!= -1) {
        i.remove();
      }
    }

    // You can print the words using for/in
    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();
    }
  }
}

           
       














Related examples in the same category
1.Foreach ArrayForeach Array
2.Simple demo to print all the types of an enumSimple demo to print all the types of an enum
3.Foreach and generic data structureForeach and generic data structure
4.Use a foreach(for each) style for loop.Use a foreach(for each) style for loop.
5.Use break with a foreach(for each) style for.Use break with a foreach(for each) style for.
6.The foreach(for each) loop is essentially read-only.
7.Use foreach(for each) style for on a two-dimensional array.
8.Search an array using foreach(for each) style for.
9.Using a foreach(for each) for loop with a collection.
10.Using a foreach(for each) for loop on an Iterable object.
11.Java for in (forin) with CollectionJava for in (forin) with Collection
12.Java for in (forin) with generic CollectionJava for in (forin) with generic Collection
13.Java for in (forin): line-by-line iteration through a text fileJava for in (forin): line-by-line iteration through a text file
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.