1 import java.util.ArrayList;
2 import java.util.Iterator;
3 import java.util.Collection;
4 import java.io.*;
5 import fi.jyu.mit.ohj2.*;
6
7
14
15 public class ArrayListMalliGen {
16
17 public static void tulosta(OutputStream os, Collection luvut) {
18 PrintStream out = Tiedosto.getPrintStream(os);
19 for (Iterator<Integer> i = luvut.iterator(); i.hasNext(); ) {
20 int luku = i.next();
21 out.print(luku + " ");
22 }
23 out.println();
24 }
25
26
27 public static void main(String[] args) {
28 ArrayList<Integer> luvut = new ArrayList<Integer>(7);
29 try {
30 luvut.add(0); luvut.add(2); luvut.add(99);
31 } catch ( Exception e ) {
32 System.out.println("Virhe: " + e.getMessage());
33 }
34 System.out.println(luvut);
35 luvut.set(1,4);
36 System.out.println(luvut);
37 int luku = luvut.get(2);
38 System.out.println("Paikassa 2 on " + luku);
39 tulosta(System.out,luvut);
40 try {
41 luvut.set(21, 4);
42 }
43 catch (IndexOutOfBoundsException e) {
44 System.out.println("Virhe: " + e.getMessage());
45 }
46 }
47 }
48