For Each Loop

/* ForEachLoop1.java */

public class ForEachLoop1
{
  public static void main(String[] args)
  {
    int[] t = {1,2,3,4};

    System.out.println("suma = " + suma(t));
    System.out.println("suma = " + sumaTiger(t));
  }
  
  static int suma(int[] t)    
  {
    int suma = 0;
    
    for (int i = 0; i < t.length; i++)
    {
      suma = suma + t[i];
    }
    
    return suma;
  }
  
  static int sumaTiger(int[] t)
  {
    int suma = 0;
    
    for (int value : t)
    {
      suma = suma + value;
    }
    
    return suma;
  }
}
suma = 10
suma = 10
Press any key to continue...
/* ForEachLoop2.java */

import java.util.*;

public class ForEachLoop2
{
  public static void main(String[] args)
  {
    Collection setI = new ArrayList();
    Collection setJ = new ArrayList();
    
    setI.add(new Element(2));
    setI.add(new Element(3));
    
    setJ.add(new Element("a"));
    setJ.add(new Element("b"));
    setJ.add(new Element("c"));
    
    iloczyn(setI, setJ);
    iloczynTiger(setI, setJ);
  }
  
  static void iloczyn(Collection c1, Collection c2)
  {
    for (Iterator i = c1.iterator(); i.hasNext(); )
    {
      Object e = i.next();
      
      for (Iterator j = c2.iterator(); j.hasNext(); )
      {
        System.out.print("(" + e + "," + j.next() + ") ");
      }
    }
    
    System.out.println();
  }

  static void iloczynTiger(Collection c1, Collection c2)
  {
    for (Object element1 : c1)
      for (Object element2 : c2)
      {
        System.out.print("(" + element1 + "," + element2 + ") ");
      }
      
    System.out.println();
  }
}

class Element
{
  String e;
  
  Element(String e)
  {
    this.e = e;
  }
  
  Element(int e)
  {
    this.e = Integer.toString(e);
  }
  
  public String toString()
  {
    return e;
  }
}
(2,a) (2,b) (2,c) (3,a) (3,b) (3,c)
(2,a) (2,b) (2,c) (3,a) (3,b) (3,c)
Press any key to continue...
Note: D:\MyProjects\Tiger\ForEachLoop2.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Process completed.
/* ForEachLoop3.java */

import java.util.*;

public class ForEachLoop3
{
  public static void main(String[] args)
  {
    Collection<Element> setI = new ArrayList<Element>();
    Collection<Element> setJ = new ArrayList<Element>();
    
    setI.add(new Element(2));
    setI.add(new Element(3));
    
    setJ.add(new Element("a"));
    setJ.add(new Element("b"));
    setJ.add(new Element("c"));
    
    iloczyn(setI, setJ);
    iloczynTiger(setI, setJ);
  }
  
  static void iloczyn(Collection<Element> c1, Collection<Element> c2)
  {
    for (Iterator<Element> i = c1.iterator(); i.hasNext(); )
    {
      Element e = i.next();
      
      for (Iterator<Element> j = c2.iterator(); j.hasNext(); )
      {
        System.out.print("(" + e + "," + j.next() + ") ");
      }
    }
    
    System.out.println();
  }

  static void iloczynTiger(Collection<Element> c1, Collection<Element> c2)
  {
    for (Element element1 : c1)
      for (Element element2 : c2)
      {
        System.out.print("(" + element1 + "," + element2 + ") ");
      }
      
    System.out.println();
  }
}

class Element
{
  String e;
  
  Element(String e)
  {
    this.e = e;
  }
  
  Element(int e)
  {
    this.e = Integer.toString(e);
  }
  
  public String toString()
  {
    return e;
  }
}
(2,a) (2,b) (2,c) (3,a) (3,b) (3,c)
(2,a) (2,b) (2,c) (3,a) (3,b) (3,c)
Press any key to continue...

Strona główna