我试图测试我的方法,但是我在使用logAsArray时遇到了问题。当我运行它时,输出返回看起来像机器代码。我想我必须使用toString(),但我无法弄清楚它放在哪里...... 测试最小时,我也得到一个空指针异常。任何想法将不胜感激。
public class hw {
public static void main(String[] args){
String name = null;
LinkedStringLog myInstance = new LinkedStringLog(name);
myInstance.insert("Bob");
myInstance.insert("Carl");
myInstance.insert("Ace");
myInstance.insert("Steve");
myInstance.insert("Mike");
myInstance.size();
int i = (myInstance.size());
while ( i != 0) {
System.out.println(myInstance.name);
System.out.println(myInstance.size());
System.out.println(myInstance.isEmpty());
System.out.println(myInstance.howMany(""));
System.out.println(myInstance.uniqInsert(""));
myInstance.smallest());
System.out.println(myInstance.logAsArray());
i--; }}
}
public String[] logAsArray(){
String values[] = new String [size()];
LLStringNode node;
node = log;
int i = 0;
while (node != null){
values[i] = node.getInfo();
node = node.getLink();
i++;
}
return values; }
public String smallest(){
LLStringNode node;
LLStringNode node2;
LLStringNode node3 = log;
node = log;
node2 = log;
String smallString = "A";
boolean notNull = (node != null);
boolean notNull2 = (node2 != null);
while (notNull && notNull2){
System.out.println(node.getInfo() + " " + node2.getInfo());
if (node.getInfo().compareTo(node2.getInfo()) <= 0) // null pointer exception
{
node3 = node;
node2 = node2.getLink();
smallString = node3.getInfo();
}
else if (notNull && notNull2)
{
node3 = node2;
node = node.getLink();
smallString = node3.getInfo();
}
smallString = node3.getInfo();
}
return smallString;}
public int size()
// Returns the number of Strings in this StringLog.
{
int count = 0;
LLStringNode node;
node = log;
while (node != null)
{
count++;
node = node.getLink();
}
return count; }
答案 0 :(得分:0)
您可能必须遍历日志并打印每个特定的字符串。
for (String s : whateverLog)
System.out.println(s);