1 import java.util.ArrayList;
2 import java.util.Vector;
3 import java.util.LinkedList;
4 import java.util.Iterator;
5 import java.util.Collection;
6 import java.util.Collections;
7 import java.util.Comparator;
8 import java.util.List;
9 import java.io.*;
10 import fi.jyu.mit.ohj2.*;
11
12
19
20 public class AlgoritmiMalliGen {
21
22
27 public static class LaskevaInt implements Comparator<Integer> {
28 public int compare(Integer o1, Integer o2) {
29 return o2 - o1;
30 }
31 }
32
33 public static void tulosta(OutputStream os, Collection<Integer> luvut) {
34 PrintStream out = Tiedosto.getPrintStream(os);
35 for (Integer i : luvut ) {
36 int luku = i;
37 out.print(luku + " ");
38 }
39 out.println();
40 }
41
42
43 public static void main(String[] args) {
44 ArrayList<Integer> luvut = new ArrayList<Integer>();
45 try {
46 luvut.add(0); luvut.add(2);
47 luvut.add(99); luvut.add(7);
48 luvut.add(22); luvut.add(71);
49 } catch ( Exception e ) {
50 System.out.println("Virhe: " + e.getMessage());
51 }
52 System.out.println(luvut);
54 Collections.sort(luvut);
55 tulosta(System.out,luvut); Collections.sort(luvut,new LaskevaInt());
57 tulosta(System.out,luvut); Collections.shuffle(luvut);
59 tulosta(System.out,luvut); Collections.sort(luvut,Collections.reverseOrder());
61 tulosta(System.out,luvut); Collections.reverse(luvut);
63 tulosta(System.out,luvut);
65 int suurin = Collections.max(luvut);
66 System.out.println("Suurin = " + suurin); int pienin = Collections.min(luvut);
68 System.out.println("Pienin = " + pienin); pienin = Collections.max(luvut,new LaskevaInt());
70 System.out.println("Pienin = " + pienin);
72 List<Integer> luvut2 = new LinkedList();
73 luvut2.addAll(0,luvut);
74 tulosta(System.out,luvut2); luvut2 = luvut.subList(2,5);
77 tulosta(System.out,luvut2);
79 }
80 }
81