Ich erstelle einen Stack von Grund auf neu und habe Schwierigkeiten beim Erstellen der toString() -Methode. Ich habe den Rest des Stapels fertiggestellt. Ich erstelle einen von Grund auf, weil es eine Aufgabe ist. Wenn ich einen Hinweis darauf bekommen könnte, was ich falsch mache, wäre das sehr hilfreich, danke !!Erstellen eines Stacks toString von Grund auf neu?
public class MyStack<Type> {
private Node top;
private int size;
private class Node{
private Node next;
private Type current;
public Node() {
next = null;
current = null;
}
}
public void push(Type item) {
Node old = top;
top = new Node();
top.current = item;
top.next = old;
size++;
}
public Type pop() {
Type type = null;
if(top != null) {
type = top.current;
top = top.next;
size--;
}
return type;
}
public Type peek(){
return top.current;
}
public int size() {
return size;
}
public boolean isEmpty(){
boolean result = false;
if(size > 0) {
result = false;
} else{
result = true;
}
return result;
}
public String toString() { // this is where I'm having issues.
String result = "[";
if(top != null) {
while(top.next != null) {
result = result + top.current;
}
}
result = result + "]";
return result;
}
}
* Wenn ich einen Hinweis darauf, was ich falsch mache bekommen kann * Sie uns nicht sagen, was das Problem ist. – Guy
@Guy Tut mir leid, also im Grunde druckt mein toString nichts aus, selbst wenn ich es anrufe, oder benutze die Push-Methode. Ich weiß, dass mein Push und Pop korrekt funktionieren, aber der toString druckt nichts aus, es geht nicht einmal in die while-Schleife. – thatsnifty
Es ist besser, das Problem in der eigentlichen Frage anzugeben. – Pullie