gpt4 book ai didi

java - Java 链表中的 NULL 值

转载 作者:行者123 更新时间:2023-12-01 14:59:43 25 4
gpt4 key购买 nike

我们正在尝试将文件中的一行输入标记为 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;
}
}

最佳答案

旁注:您可以在多个地方编写如下代码:

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

将它们替换为

while (big_long_test) {
do_something();
}

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

关于java - Java 链表中的 NULL 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13848662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com