001 import java.util.*;
002 import java.io.*;
003 import fi.jyu.mit.ohj2.*;
004
005 /**
006 * Esimerkki Javan vektorin käytöstä Java 1.5:n geneerisyyden
007 * ja "autoboxin" avulla. Käytössä myös uusi for-silmukka.
008 * @author Vesa Lappalainen
009 * @version 1.0, 02.03.2002
010 * @version 1.1, 01.03.2005
011 */
012
013 public class VectorMalliGen {
014
015 public static void tulosta(OutputStream os, Vector luvut) {
016 PrintStream out = Tiedosto.getPrintStream(os);
017 for (Iterator<Integer> i = luvut.iterator(); i.hasNext(); ) {
018 int luku = i.next();
019 out.print(luku + " ");
020 }
021 out.println();
022 }
023
024 public static void tulosta2(OutputStream os, Collection<Integer> luvut) {
025 PrintStream out = Tiedosto.getPrintStream(os);
026 for (Integer i : luvut ) {
027 out.print(i + " ");
028 }
029 out.println();
030 }
031
032 public static void tulosta3(OutputStream os, Collection<?> luvut) {
033 PrintStream out = Tiedosto.getPrintStream(os);
034 for (Object i : luvut ) {
035 out.print(i + " ");
036 }
037 out.println();
038 }
039
040 public static void tulosta4(OutputStream os, Collection luvut) {
041 PrintStream out = Tiedosto.getPrintStream(os);
042 for (Object i : luvut ) {
043 out.print(i + " ");
044 }
045 out.println();
046 }
047
048 public static void main(String[] args) {
049 Vector<Integer> luvut = new Vector<Integer>(7);
050 try {
051 luvut.add(0); luvut.add(2);
052 luvut.add(99);
053 } catch ( Exception e ) {
054 System.out.println("Virhe: " + e.getMessage());
055 }
056 System.out.println(luvut);
057 luvut.set(1,4);
058 System.out.println(luvut);
059 int luku = luvut.get(2);
060 System.out.println("Paikassa 2 on " + luku);
061 tulosta(System.out,luvut);
062 tulosta2(System.out,luvut);
063 tulosta3(System.out,luvut);
064 tulosta4(System.out,luvut);
065 try {
066 luvut.set(21, 4);
067 }
068 catch (IndexOutOfBoundsException e) {
069 System.out.println("Virhe: " + e.getMessage());
070 }
071 }
072 }