1   
2   
3   import fi.jyu.mit.ohj2.Tiedosto;
4   import java.util.Iterator;
5   import java.util.Collection;
6   import java.io.OutputStream;
7   import java.io.PrintStream;/**
8    * Esimerkki dynaamisesta taulukosta
9    * @author Vesa Lappalainen
10   * @version 1.0, 02.03.2002
11   */
12  
13  public class TaulukkoIter {
14    public class TaulukkoIterTaysiException extends Exception {
15      TaulukkoIterTaysiException(String viesti) { super(viesti); }
16    }
17  
18    private Integer alkiot[];
19    private int lkm;
20  
21    public TaulukkoIter() {
22      alkiot = new Integer[4];
23      lkm = 0;
24    }
25  
26    public TaulukkoIter(int koko) {
27      alkiot = new Integer[koko];
28      lkm = 0;
29    }
30  
31    public void lisaa(Integer i) throws TaulukkoIterTaysiException {
32      if ( lkm >= alkiot.length )  throw new TaulukkoIterTaysiException("Tila loppu");
33      alkiot[lkm] = i;
34      lkm++;
35    }
36  
37    public String toString() {
38      StringBuffer s = new StringBuffer("");
39      for (int i=0; i<lkm; i++)
40        s.append(" " + alkiot[i]);
41      return s.toString();
42    }
43  
44    public void set(int i, Integer luku) throws IndexOutOfBoundsException {
45      //if ( ( i < 0 ) || ( lkm <= i ) ) throw new IndexOutOfBoundsException("i = " + i);
46      alkiot[i] = luku;
47    }
48  
49    public Integer get(int i) throws IndexOutOfBoundsException {
50      if ( ( i < 0 ) || ( lkm <= i ) ) throw new IndexOutOfBoundsException("i = " + i);
51      return alkiot[i];
52    }
53  
54    public static void tulosta(OutputStream os,  TaulukkoIter luvut) {
55      PrintStream out = Tiedosto.getPrintStream(os);
56      for (Iterator i = luvut.iterator(); i.hasNext(); ) {
57        int luku = ((Integer)i.next()).intValue();
58        out.print(luku + " ");
59      }
60      out.println();
61    }
62  
63  
64    public class Iter implements Iterator {
65      int kohdalla;
66  
67      public Iter() {
68        kohdalla = -1;
69      }
70  
71      public boolean hasNext() {
72        return kohdalla < lkm-1;
73      }
74  
75      public Object next() {
76        return alkiot[++kohdalla];
77      }
78  
79      public void remove() {
80  
81      }
82  
83    }
84  
85    public Iterator iterator() {
86      return new Iter();
87    }
88  
89    public static void main(String[] args) {
90     /*
91      Integer i1 = new Integer(5);
92      Integer i2 = new Integer(6);
93      System.out.println(i1);
94      i2 = new Integer(4);
95      i2 = 4;
96      int i3 = i2.intValue();
97      i2 = i1 * i2;
98      i2 = new Integer(i1.intValue() + i2.intValue());
99      System.out.println(i2);
100 
101  if ( true ) return;
102   */
103     TaulukkoIter luvut = new TaulukkoIter(100);
104     try {
105       luvut.lisaa(0); // luvut.lisaa(new Integer(0));
106       luvut.lisaa(2);
107       luvut.lisaa(99);
108     } catch ( TaulukkoIterTaysiException e ) {
109       System.out.println("Virhe: " + e.getMessage());
110     }
111     System.out.println(luvut);
112     luvut.set(1,4); // luvut.set(1, new Integer(4));
113     System.out.println(luvut);
114     int luku = luvut.get(2);
115     System.out.println("Paikassa 2 on " + luku);
116 
117     tulosta(System.out,luvut);
118 
119     try {
120       luvut.set(21, 4);
121     }
122     catch (IndexOutOfBoundsException e) {
123       System.out.println("Virhe: " + e.getMessage());
124     }
125   }
126 }
127