Created
May 10, 2015 12:37
-
-
Save 3c7/050939d3b99b4d05aebf to your computer and use it in GitHub Desktop.
Algorithmen und Datenstrukturen Übung 5 Aufgabe 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.NoSuchElementException; | |
/** | |
* Created by Nils on 28.04.2015. | |
*/ | |
public class Feld<T> implements Iterable<T> { | |
private T[] feld; | |
@SuppressWarnings("unchecked") | |
public Feld(int anzahl) { | |
T[] f = (T[]) new Object[anzahl]; | |
feld = f; | |
} | |
public int size() { | |
return feld.length; | |
} | |
public T get(int i) throws ArrayIndexOutOfBoundsException { | |
return feld[i]; | |
} | |
public T set(int i, T e) { | |
T result = feld[i]; | |
feld[i] = e; | |
return result; | |
} | |
public Iterator iterator() { | |
return new Iterator(); | |
} | |
class Iterator implements java.util.Iterator<T> { | |
private int counter = 0; | |
public boolean hasNext() { | |
if (size() > counter) return true; | |
return false; | |
} | |
public T next() throws NoSuchElementException { | |
try { | |
return get(this.counter); | |
} catch (ArrayIndexOutOfBoundsException oobe) { | |
throw new NoSuchElementException(); | |
} finally { | |
this.counter++; | |
} | |
} | |
public void remove() throws UnsupportedOperationException { | |
throw new UnsupportedOperationException(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Nils on 28.04.2015. | |
*/ | |
@SuppressWarnings("unchecked") | |
public class Paar<T> { | |
private T first, second; | |
public Paar(T f, T s) { | |
this.first = f; | |
this.second = s; | |
} | |
public void swap() { | |
T tmp; | |
tmp = this.first; | |
this.first = this.second; | |
this.second = tmp; | |
} | |
public T first() { | |
return this.first; | |
} | |
public T second() { | |
return this.second; | |
} | |
public String toString() { | |
return "(" + this.first() + "," + this.second() + ")"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Iterator; | |
import java.util.NoSuchElementException; | |
/** | |
* Created by Nils on 28.04.2015. | |
*/ | |
public class PaarFeld<T> implements Iterable<Paar<T>> { | |
private Feld<T> a, b; | |
private int length; | |
public PaarFeld(Feld<T> a, Feld<T> b) { | |
this.length = (a.size() < b.size()) ? a.size() : b.size(); | |
this.a = a; | |
this.b = b; | |
} | |
public Paar<T> get(int i) throws ArrayIndexOutOfBoundsException { | |
if (i < length) { | |
return new Paar<T>(a.get(i), b.get(i)); | |
} else { | |
throw new ArrayIndexOutOfBoundsException(); | |
} | |
} | |
public Paar<T> set(int i, Paar<T> a) { | |
return new Paar<T>(this.a.set(i, a.first()), this.b.set(i, a.second())); | |
} | |
public int size() { | |
return this.length; | |
} | |
@SuppressWarnings("unchecked") | |
public Iterator iterator() { | |
return new Iterator(); | |
} | |
// Eingebettete Iterator-Klasse | |
class Iterator implements java.util.Iterator<Paar<T>> | |
{ | |
private int counter = 0; | |
public boolean hasNext() { | |
if (size() > counter) return true; | |
return false; | |
} | |
@SuppressWarnings("unchecked") | |
public Paar<T> next() throws NoSuchElementException { | |
try { | |
return get(counter); | |
} catch (ArrayIndexOutOfBoundsException oob) { | |
throw new NoSuchElementException(); | |
} finally { | |
counter++; | |
} | |
} | |
public void remove() throws UnsupportedOperationException { | |
throw new UnsupportedOperationException(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment