CirculaListクラスのオリジナルのtoStringメソッドが正常に動作します。しかし、私は私のスタッククラスの私のtoStringメソッドでそれを呼び出すとき、私は私のスタックのメモリ位置ですと仮定して返す。私はそれを過負荷にしようとしましたが、区別できる差異はありませんでした。文字列を返す文字列へのJavaカスタム
マイコード:
CircularListクラス
public class CircularList{
private Node tail = null;
public void addToFront(int elem){
if(elem >= 0){
Node newNode= new Node(elem);
if(tail != null){
newNode.next = tail.next;
tail.next = newNode;
}else{
tail = newNode;
newNode.next = newNode;
}
}
}
public int deleteFromBack(){
int returned = -1;
if(tail != null){
Node trav = tail;
while(trav.next != tail){
trav = trav.next;
}
returned = tail.data;
if(trav.next == trav){
tail = null;
}else{
trav.next = tail.next;
tail = trav;
}
}
return returned;
}
//@Override
public String toSting(){
String stg = null;
stg = "[";
Node trav = tail.next;
while(trav != tail){
stg += trav.getData() +",";
trav = trav.next;
}
stg += trav.getData() + "]";
return stg;
}
public CircularList clone(CircularList other){
tail = other.tail;
return this;
}
}
スタッククラス
public class Stack{
private CircularList list = new CircularList();
public void push(int elem){
list.addToFront(elem);
}
public int pop(){
CircularList tempList = new CircularList();
int keeper = -1;
if(isEmpty() != true){
keeper =list.deleteFromBack();
while(isEmpty() != true){
tempList.addToFront(keeper);
keeper =list.deleteFromBack();
}
list.clone(tempList);
}
return keeper;
}
public int peek(){
CircularList tempList = new CircularList();
int keeper = -1;
if(isEmpty() != true){
while(!isEmpty()){
keeper = list.deleteFromBack();
tempList.addToFront(keeper);
}
list.clone(tempList);
}
return keeper;
}
public boolean isEmpty(){
CircularList tempList = new CircularList();
int keeper = -1;
keeper = list.deleteFromBack();
if(keeper==-1){
return true;
}else{
while(keeper!=-1){
tempList.addToFront(keeper);
keeper = list.deleteFromBack();
}
list.clone(tempList);
}
return false;
}
public Stack clone(Stack other){
list = other.list;
return this;
}
//@Override
public String toSting(){
String stg = list.toString();
return stg;
}
}
テスタークラス(名の通り、それはテストのために純粋であることを示唆している)
public class Tester{
public static void main(String[] args)
{
CircularList list = new CircularList();
System.out.println("CircularList");
System.out.println("=========");
list.addToFront(1);
list.addToFront(2);
list.addToFront(3);
list.addToFront(4);
list.addToFront(5);
System.out.println(list.toSting());
System.out.println("=========");
System.out.println("removing: " + list.deleteFromBack());
System.out.println("=========");
System.out.println(list.toSting());
System.out.println("=====================================================");
System.out.println("stack");
System.out.println("=========");
Stack stack = new Stack();
System.out.println(stack.isEmpty());
System.out.println("=========");
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
System.out.println(stack.toString());
System.out.println(stack.isEmpty());
System.out.println("=========");
System.out.println("first int: " + stack.peek());
System.out.println("=========");
System.out.println("Removing: " + stack.pop());
System.out.println(stack.isEmpty());
}
}
除去出力(それらはレイアウトをいじったとして取り出さ等しい) CircularList
[5,4,3,2,1]
:1
を[ 5,4,3,2]
スタック
true
15db9742 偽
最初のint @スタック:削除5
:5 偽
'toSting'は' toString'ではありません –
Typo:toSting()! –
私は他の3人に助けを求めます。私たちの誰もこれを見つけることはできませんでした。ありがとう。 –