Java中链表中的空值

Java中链表中的空值,java,linked-list,Java,Linked List,我们试图将文件中的一行输入标记化为ADTList“tokens”。它正在读取信息,但列表中的第一个对象被设置为NULL。我们已经尝试了许多改变,但无法找出这是在哪里发生的。代码如下: //method to covernt lines of input from a file to individual tokens public static ADTList tokenizer(String input){ String aToken;

我们试图将文件中的一行输入标记化为ADTList“tokens”。它正在读取信息,但列表中的第一个对象被设置为NULL。我们已经尝试了许多改变,但无法找出这是在哪里发生的。代码如下:

    //method to covernt lines of input from a file to individual tokens
public static ADTList tokenizer(String input){

    String aToken;                                                      //defining variable to create token
    ADTList tokens = new ADTList();                                     //defining return variable

for (int i = 0; i < input.length(); i++){                           //loop to iterate through input string
        aToken = "";                                                    //clearing variable to hold next string
        if (input.charAt(i) == '(' || input.charAt(i) == ')' ||
                operator(input.charAt(i))){                             //testing for parenthesis or operator
            aToken += input.charAt(i);                                  //concatenating character to string
            tokens.add(aToken);                                         //adding new string to token list
        }else if (Character.isLetter(input.charAt(i))){                 //testing for variable letter
            aToken += input.charAt(i);
    i++;                                                        //moving to next character in string
            if (i < input.length() && Character.isLetterOrDigit(input.charAt(i))){   
                do{                                                     //loop to test for end of variable
                    aToken += input.charAt(i);
                    i++;
                }while (i < input.length() && Character.isLetterOrDigit(input.charAt(i)));    //end of variable condition
            }
            tokens.add(aToken);
            i--;                                                        //decrementing counter variable
        }else if (Character.isDigit(input.charAt(i))){                  //testing for number
    aToken += input.charAt(i);
    i++;
                if (i < input.length() && Character.isDigit(input.charAt(i))){   
                do{                                                     //loop to test for end of variable
                    aToken += input.charAt(i);
                    i++;
                }while (i < input.length() && Character.isDigit(input.charAt(i)));    //end of digit condition
            }
            tokens.add(aToken);
            i--;
        }else{
    //do nothing
        }//end if
}//end loop

return tokens;                                                      //returns tokens list to main
}//endFunction
//将文件中的输入行覆盖到单个标记的方法
公共静态ADTList标记器(字符串输入){
String aToken;//定义变量以创建令牌
ADTList tokens=new ADTList();//定义返回变量
for(int i=0;i
以下是在第一个位置显示空对象的输出快照:

  • 原始功能:a1*(b+c)
  • 标记化:null,a1,*,(,b,+,c,)
  • 中缀到后缀:
  • 中缀后缀:

  • 原始功能:(a*b+c)

  • 标记化:null,(,a,*,b,+,c
  • 中缀到后缀:
  • 中缀后缀:
这是ADTList类。这不是全部,但它确实显示了主项目调用的add(T item)方法:

public class ADTList<T> implements ListInterface<T>{

//defining internal variables to be used in interface class
private int numItems;
private T array[];

public ADTList(){
    //create an empty array
    array = (T[]) new Object [25];
    //assign 0 to numItems
    numItems=0;
}


public boolean isEmpty(){
// Determines whether a list is empty.
// Precondition: None.
// Postcondition: Returns true if the list is empty,
//                otherwise returns false.
    if (numItems==0){
        return true;
    }else{
        return false;
    }
}

public boolean isFull(){
// Determines whether a list is full.
// Precondition: None.
// Postcondition: Returns true if the list is full,
//                otherwise returns false.
    return numItems==array.length;
}

public int size(){
// Determines the length of a list.
// Precondition: None.
// Postcondition: Returns the number of items that are
//                currently in the list.
    return numItems;
}


public boolean add(T item){
// Adds an item to the end of list.
// Precondition: the item should be inserted in the list.
// Postcondition: If insertion is successful, it returns true,
//               item is at the end of the list,
//               Returns false if the list is full
    if (isFull()){
        return false;
    }else {
        array[numItems]=item;
        numItems++;
        return true;
    }
}
public类ADTList实现ListInterface{
//定义要在接口类中使用的内部变量
私有内部网络;
专用T数组[];
公共广告列表(){
//创建一个空数组
数组=(T[])新对象[25];
//将0分配给numItems
numItems=0;
}
公共布尔值为空(){
//确定列表是否为空。
//前提条件:无。
//Postcondition:如果列表为空,则返回true,
//否则返回false。
如果(numItems==0){
返回true;
}否则{
返回false;
}
}
公共布尔值isFull(){
//确定列表是否已满。
//前提条件:无。
//Postcondition:如果列表已满,则返回true,
//否则返回false。
返回numItems==array.length;
}
公共整数大小(){
//确定列表的长度。
//前提条件:无。
//Postcondition:返回要删除的项目数
//目前在列表中。
返回numItems;
}
公共布尔加法(T项){
//将项目添加到列表的末尾。
//前提条件:项目应插入列表中。
//后置条件:如果插入成功,则返回true,
//项目位于列表的末尾,
//如果列表已满,则返回false
如果(isFull()){
返回false;
}否则{
数组[numItems]=项;
numItems++;
返回true;
}
}

旁注:您可以在几个地方编写这样的代码:

if (big_long_test) {
    do {
        something();
    } while(big_long_test);
}
将这些替换为

while (big_long_test) {
    do_something();
}

上面的代码没有向列表中添加null。您的问题可能在ADTList类中。

不知道ADTList是如何实现的(内部使用ArrayList?),如果第一个位置的对象是空字符串,则trim()您从文件中读取的字符串输入变量,以确保没有前导空格。如果输入有前导空格,您将向ADTList添加一个空值。

我认为问题在于调用构造函数时,ADTList类使列表的第一个成员为空。您提供的代码没有问题。

nk为您输入所有内容。错误在于打印出信息。get()函数提取数组(位置1),因此,我们的排名是-1。我非常感谢这些建议和提供的代码精简。

我怀疑问题出在ADTList类中,您是否也可以发布该类的代码?您是否可以使用ArrayList来代替?@PeterLawrey:不,我们必须使用在类中讨论和编码的ADTList类。您是否已采取步骤我会在ADTList::add中设置一个断点,并观察当numItems应该为0时第一次通过时会发生什么