0

ファイルからの入力行を ADTList の「トークン」にトークン化しようとしています。情報は正常に読み取られていますが、リストの最初のオブジェクトが 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

最初の位置に NULL オブジェクトを示す出力のスナップショットを次に示します。

  • 元の関数: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;
    }
}
4

4 に答える 4

1

補足: 次のようなコードを記述する場所がいくつかあります。

if (big_long_test) {
    do {
        something();
    } while(big_long_test);
}

これらを

while (big_long_test) {
    do_something();
}

上記のコードは、リストに null を追加しません。問題はおそらく ADTList クラスにあります。

于 2012-12-12T21:17:09.197 に答える
0

最初の位置のオブジェクトが空の文字列の場合、ADTList がどのように実装されているかわかりません (内部で ArrayList を使用しますか?) ファイルから読み取った文字列入力変数をトリム() して、先頭に空白がないことを確認します。入力の先頭に空白がある場合は、ADTList に空の値を追加します。

于 2012-12-12T21:18:11.037 に答える
0

問題は、コンストラクターが呼び出されたときに ADTList クラスがリストの最初のメンバーを null にすることだと思います。ご提示いただいたコードで問題ありません。

于 2012-12-12T21:19:25.630 に答える
0

ご意見ありがとうございます。エラーは情報の印刷にありました。get() 関数は array(position-1) をプルするため、-1 の位置をプルしていました。提供された提案とコードの合理化に感謝します。

于 2012-12-13T01:09:14.800 に答える