Hallo Leute,
ich habe folgende Klassen, die auf jeden Fall korrekt sind:
Ich hab mir noch eine zusätzliche Klasse ausgedacht:
von der ich mir aber nicht sicher bin, ob Sie korrekt arbeitet.
Meine Klasse Person sieht so aus:
So und jetzt will ich testen:
Das Ergebnis müssten die beiden Personen Geilo mat und Hans Leberwurst sein, aber die Ausgabe ist:
Person@7a7b0070
Person@7a7b0070
Ich hab in früheren Beiträgen nach einem ähnlichen Problem gesucht, aber doch nichts gefunden. Hat jemand einen Tipp, was ich hier übersehen haben könnte?
ich habe folgende Klassen, die auf jeden Fall korrekt sind:
Java:
class Entry {
Person element;
Entry next;
Entry previous;
Entry(Person element, Entry next, Entry previous) {
this.element = element;
this.next = next;
this.previous = previous;
}
}
class LinkedList1618 {
Entry header = new Entry(null, null, null);
int size = 0;
/* Constructs an empty Linked List. */
LinkedList1618() {
header.next = header;
header.previous = header;
}
/* Returns the last Element in this List. */
Person getLast() {
if (size == 0) throw new java.util.NoSuchElementException();
return header.previous.element;
}
/* Removes and returns the last Element from this List. */
Person removeLast() {
Entry lastEntry = header.previous;
if (lastEntry == header) throw new java.util.NoSuchElementException();
lastEntry.previous.next = lastEntry.next;
lastEntry.next.previous = lastEntry.previous;
size--;
return lastEntry.element;
}
/* Appends the given element to the end of this List. */
void addLast(Person p) {
Entry newEntry = new Entry(p, header, header.previous);
header.previous.next = newEntry;
header.previous = newEntry;
size++;
}
/* Returns the number of elements in this List. */
int size() {
return size;
}
}
Java:
/* Returns the element at given position of this List*/
Person getElementAt(int position){
Entry elementAt = header.next;
if (position <= size) {
for(int n=0;n<=position;n++) {
elementAt =elementAt.next;
}
return elementAt.element;
}
else {
throw new java.util.NoSuchElementException();
}
}
Meine Klasse Person sieht so aus:
Java:
class Person {
String firstName; // Attribut
String lastName; // Attribut
Person(String firstName, String lastName) { // Konstruktor
this.firstName = firstName;
this.lastName = lastName;
}
String getLastName() { // Methode
return lastName;
}
String getFirstName() { // Methode
return firstName;
}
}
Java:
public class TestLinkedList1618 {
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedList1618 liste = new LinkedList1618();
liste.addLast(new Person("Geilo", "mat"));
liste.addLast(new Person("Hans", "Leberwurst"));
System.out.println(liste.getElementAt(0));
System.out.println(liste.getLast());
}
}
Person@7a7b0070
Person@7a7b0070
Ich hab in früheren Beiträgen nach einem ähnlichen Problem gesucht, aber doch nichts gefunden. Hat jemand einen Tipp, was ich hier übersehen haben könnte?