Moin,
ich habe die Klassen ```import java.util.NoSuchElementException;
/**
- A simple linked list. One may go through this list by {@link #advance()} until
- the last position ({@link #endpos()}) is reached. One also may
- {@link #delete()} and {@link #insert(Object)} elements. After advancing it is
- possible to go back to the beginning by {@link #reset()}.
*/
public class List {
/**
* Reference on the first Entry of this List
/
private Entry begin;
/*
* References before the actual Entry of this List
*/
private Entry pos;
/**
* Create a new empty List.
*/
public List() {
pos = begin = new Entry();
}
/**
* Determines if this List is empty or not.
*
* @return true
, if there are no elements in this List
*/
public boolean empty() {
return begin.next == null;
}
/**
* Determines if it is possible to {@link #advance()} in this List. Returns
* true
if the last position of this List has been reached. An
* {@link #empty()} List will alway deliver true
*
* @return true
if the last Entry in this List already has been
* reached.
*/
public boolean endpos() { // true, wenn am Ende
return pos.next == null;
}
/**
* Returns to the beginning of this List.
*/
public void reset() {
pos = begin;
}
/**
* Advances one step in this List.
*
* @throws NoSuchElementException
* if the last Entry of this List already has been reached.
*/
public void advance() {
if (endpos()) {
throw new NoSuchElementException(“Already at the end of this List”);
}
pos = pos.next;
}
/**
* Returns the actual element of this List.
*
* @return the actual element
*
* @throws NoSuchElementException
* if the last Entry of this List already has been reached.
*/
public Object elem() {
if (endpos()) {
throw new NoSuchElementException(“Already at the end of this List”);
}
return pos.next.o;
}
/**
* Inserts o
in this List. It will be placed before the actual
* element. After insertion the inserted element will become the actual
* element.
*
* @param x
* the element to be inserted
*/
public void add(Object x) {
Entry newone = new Entry(x, pos.next);
pos.next = newone;
}
/**
* Deletes the actual element of this List. The element after the actual
* element will become the new actual element.
*
* @throws NoSuchElementException
* if the last Entry of this List already has been reached.
*/
public void delete() {
if (endpos()) {
throw new NoSuchElementException(“Already at the end of this List”);
}
pos.next = pos.next.next;
}
}```
und
/**
* An Entry holds an Object <code>o</code> and a reference <code>next</code> to
* the next Entry such that a linked List of Entry elements is generated.
*
*
*/
class Entry {
Object o;
Entry next;
Entry() {
this(null, null);
}
Entry(Object o) {
this(o, null);
}
Entry(Object o, Entry e) {
this.o = o;
this.next = e;
}
}
``` und ich soll jetzt eine typsichere Liste implementieren.
Meine erste Frage lautet ist es sinnvoller von der bestehenden Liste zu erben oder eine komplett neue Liste zu schreiben??
Meine erste Intuition ist "erben" da man sich warscheinlich Schreibarbeit spart?!?
mit freundlichen Grüßen:)