1   import java.util.*;
2   import java.io.*;
3   import fi.jyu.mit.ohj2.*;
4   
5   /**
6    * Esimerkki Javan vektorin käytöstä Java 1.5:n geneerisyyden
7    * ja "autoboxin" avulla.  Käytössä myös uusi for-silmukka.
8    * @author Vesa Lappalainen
9    * @version 1.0, 02.03.2002
10   * @version 1.1, 01.03.2005
11   */
12  
13  public class VectorMalliGen {
14  
15    public static void tulosta(OutputStream os,  Vector luvut) {
16      PrintStream out = Tiedosto.getPrintStream(os);
17      for (Iterator<Integer> i = luvut.iterator(); i.hasNext(); ) {
18        int luku = i.next();
19        out.print(luku + " ");
20      }
21      out.println();
22    }
23  
24    public static void tulosta2(OutputStream os,  Collection<Integer> luvut) {
25      PrintStream out = Tiedosto.getPrintStream(os);
26      for (Integer i : luvut ) {
27        out.print(i + " ");
28      }
29      out.println();
30    }
31  
32    public static void tulosta3(OutputStream os,  Collection<?> luvut) {
33      PrintStream out = Tiedosto.getPrintStream(os);
34      for (Object i : luvut ) {
35        out.print(i + " ");
36      }
37      out.println();
38    }
39  
40    public static void tulosta4(OutputStream os,  Collection luvut) {
41      PrintStream out = Tiedosto.getPrintStream(os);
42      for (Object i : luvut ) {
43        out.print(i + " ");
44      }
45      out.println();
46    }
47  
48    public static void main(String[] args) {
49      Vector<Integer> luvut = new Vector<Integer>(7);
50      try {
51        luvut.add(0); luvut.add(2);
52        luvut.add(99);
53      } catch ( Exception e ) {
54        System.out.println("Virhe: " + e.getMessage());
55      }
56      System.out.println(luvut);
57      luvut.set(1,4);
58      System.out.println(luvut);
59      int luku = luvut.get(2);
60      System.out.println("Paikassa 2 on " + luku);
61      tulosta(System.out,luvut);
62      tulosta2(System.out,luvut);
63      tulosta3(System.out,luvut);
64      tulosta4(System.out,luvut);
65      try {
66        luvut.set(21, 4);
67      }
68      catch (IndexOutOfBoundsException e) {
69        System.out.println("Virhe: " + e.getMessage());
70      }
71    }
72  }
73