class NichtGefunden extends Exception{ private static final long serialVersionUID = 7526471155622776147L; private String message; public NichtGefunden(String meldung){ message = meldung; } public String getMessage(){ return message;} } // Ende der Klasse NixDaException class Element{ int inhalt; Element(int i){ inhalt = i;} } class MyRandom{ static private int max = 52; static private java.util.Random RGen = new java.util.Random(); static void setMax( int m){ max = m;} static Element zufall(){ int i = Math.abs(RGen.nextInt()); return new Element(i % max); } } class ArraySuche { final int anzahl = 42; Element[] cont = new Element[anzahl]; ArraySuche(){ for (int i=0; i < cont.length; i++) cont[i] = MyRandom.zufall(); } boolean P(Element e){return e.inhalt == 42;} int linSuche()throws NichtGefunden{ return linSuche(cont);} int linSuche(Element[] a)throws NichtGefunden{ for (int i=0; i < a.length; i++) if (P(a[i])) return i; throw new NichtGefunden ("Nix da"); } } class LinSuche4 { public static void main(String ... args) { System.out.println("Lineare Suche 4"); ArraySuche L = new ArraySuche(); try { int index = L.linSuche(); System.out.println(" gefunden auf Position: "+index); } catch(NichtGefunden e){System.out.println(e.getMessage()); } } }