【问题标题】:IndexOutOfBoundsException error adding long numbersIndexOutOfBoundsException 错误添加长数字
【发布时间】:2014-09-29 21:24:55
【问题描述】:

我正在尝试使用 LinkedLists 编写最多可添加 5 个长数字的 Java 应用程序。在运行结束时我得到这个:

线程“主”java.lang.IndexOutOfBoundsException 中的异常:索引: 0,尺寸:0 在 java.util.LinkedList.checkElementIndex(LinkedList.java:555) 在 java.util.LinkedList.remove(LinkedList.java:525) 在 Assignment1.LongNumbers.remove(LongNumbers.java:33) 在 Assignment1.LongNumbers.main(LongNumbers.java:92)

代码如下:

import java.util.*;
/**
*
* @author .....
*/

public class LongNumbers 
{ 
private List<Integer> [] theLists; 
public LongNumbers() { 
    this.theLists = new LinkedList[6]; 
    for (int i=0; i<6; i++) 
    this.theLists[i]= new LinkedList<>(); 
} 

public void add(int location, int digit) { 
    //add digit at head of LinkedList given by location 
    theLists[location].add(digit);
} 

public int remove(int location) { 
    //remove a digit from LinkedList given by location
    return theLists[location].remove(location);  //LongNumbers.java:33
} 

public boolean isEmpty(int location) { 
    //check for an empty LinkedList given by location
    return theLists[location].isEmpty();
} 

public static void main(String[] args) { 
    Scanner stdIn = new Scanner(System.in);

     //Local Variables
    int digit;
    int carry = 0;
    int numberAt = 0;
    int largestNumLength = 0;
    char[] digits;
    String number;
    boolean userWantstoQuit = false;
    LongNumbers Lists = new LongNumbers();

    System.out.println("The program will enter up to 5 numbers and add them up.");
    System.out.println();

    while(!userWantstoQuit && numberAt != 5){
        System.out.print("Enter a number, enter -1 to quit entry phase: ");
        number = stdIn.nextLine();

        if((number.compareTo("-1")) == 0)
            userWantstoQuit = true;
        else{
            digits = new char[number.length()];
            for(int i=0;i<number.length();i++)
                digits[i] = number.charAt(i);
            for(int i=0;i<number.length();i++){
                int tempValue = digits[i] - 48;
                try{
                    Lists.add(numberAt, tempValue);
                }
                    catch(NumberFormatException nfe){
                        System.out.println("Invalid Input. Please try again.");
                        break;
                    }
                if(i == (number.length() - 1))
                    numberAt++;
                if(number.length() > largestNumLength)
                    largestNumLength = number.length();
            }
        }
    }

    for(int j=0;j<largestNumLength;j++){
        int tempDigit = 0;
        int index = 0;

        while(index < numberAt){
            if(Lists.theLists[index].get(0) != null){
                tempDigit += Lists.theLists[index].get(0);
                Lists.remove(0);  //LongNumbers.java:99                        
            }
        index++;
        }

        digit = carry + tempDigit;

        if(j < numberAt){
            carry = digit/10;
            digit = digit%10;
        }
    Lists.add(5, digit);
    }

    System.out.print("The sum of the numbers is: ");

    for(int i=0;i<Lists.theLists[5].size();i++){
        System.out.print(Lists.theLists[5].get(i));
    }

    System.out.println();
    System.out.println();
    System.out.println();

}//end main 
}//end class

【问题讨论】:

  • 如果您能在代码中指出“LongNumbers.java:33”这一行是什么就太好了
  • 很抱歉。已进行编辑
  • 您的堆栈跟踪非常清楚地告诉您,在您的 remove() 方法中,您正在请求大小为零的列表的索引 0 处的元素 - 即空的。看看你是如何初始化你的列表的。

标签: java linked-list indexoutofboundsexception


【解决方案1】:

对于初学者,我认为您不能拥有 List&lt;E&gt; 对象的数组...

您还应该确保您的列表已初始化并且在给定的location 中有一个项目。

所以你的方法可能看起来像这样:

public int remove(int location)
{ 
     if(theLists != null)
          if(theLists.size() > location)
               return theLists.remove(location);
     return 0;
}

如果您需要二维列表,您可以尝试使用List&lt;List&lt;E&gt;&gt;

将所有E 视为Integer

【讨论】:

    【解决方案2】:

    看这里的代码:

    while(index < numberAt){
                if(Lists.theLists[index].get(0) != null){
                    tempDigit += Lists.theLists[index].get(0);
                    Lists.remove(0);  //LongNumbers.java:99                        
                }
            index++;
            }
    

    您正在检查index'th 列表的第一个元素是否不为空。如果这是真的,您将添加它并调用remove 方法。但是,如果您已经处理了第一个列表并且index'th 的值为 1,该怎么办?在这种情况下,theLists[1].get(0) != null 为真,但 Lists.remove(0) 将 0 传递为 location。看看这段代码:

    public int remove(int location) { 
        //remove a digit from LinkedList given by location
        return theLists[location].remove(location);  //LongNumbers.java:33
    } 
    

    在我描述的场景中,location 为 0。但您的第 0 个列表已经为空...

    编辑:重写remove 方法,如下所示:

    public int remove(int location, int index) { 
        //remove a digit from LinkedList given by location
        return theLists[index].remove(location);  //LongNumbers.java:33
    } 
    

    每当您调用此方法时,请传递要使用的列表的index。示例:

    while(index < numberAt){
                if(Lists.theLists[index].get(0) != null){
                    tempDigit += Lists.theLists[index].get(0);
                    Lists.remove(0, index);  //LongNumbers.java:99                        
                }
            index++;
            }
    

    最后:在未来,请结构化你的代码,在这种非结构化状态下阅读它真的很痛苦,阅读如何编码。

    【讨论】:

      猜你喜欢
      • 2015-11-07
      • 1970-01-01
      • 1970-01-01
      • 2012-05-03
      • 1970-01-01
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      • 2020-05-16
      相关资源
      最近更新 更多