鏈表我有一個IntNode類,看起來像這樣:,代表了許多
public class IntNode {
private int value;
private IntNode next;
public IntNode(int val, IntNode n)
{
value = val;
next = n;
}
public void setValue(int val){ value = val; }
public void setNext(IntNode n){ next = n; }
public int getValue() { return value; }
public IntNode getNext() { return next; }
}
和另一個類,我把它命名爲BigNumber應表示任何正數(或大或小)。這個類看起來像:
public class BigNumber {
private IntNode list;
//copy constructor.
public BigNumber(BigNumber other){
list = null;
for(IntNode p=other.list; p!=null; p=p.getNext())
list = new IntNode(p.getValue(), list);
}
//Constructor that takes string of a number and puts every digit in the linked list. if the string contains any char that is not digit, the linked list should be: 0->null.
public BigNumber(String num){
list = null;
if(stringIsNum(num)){
for(int i=0; i<num.length(); i++){
list = new IntNode((int)num.charAt(i),list);
}
}
else{
list = new IntNode(0, list);
}
}
private boolean stringIsNum(String num){
for(int i=0; i<num.length(); i++){
if(!(num.charAt(i)>='0' && num.charAt(i)<='9'))
return false;
}
return true;
}
public String toString(){
String s = "";
for(IntNode p=list; p!=null; p=p.getNext())
s+=p.getValue()+"";
return s;
}
}
這一類的問題是,當我要打印,可以說該字符串爲「123」,它打印像515049而不是實際的數字是321,(其應該向後打印數字)。 問題是什麼?
這意味着該學習使用調試器了,因爲這應該1)幫助您自己找到答案,並且2)讓您深入瞭解如何使用強大的工具。 –