IndexOutOfBoundsException错误添加长号

时间:2014-09-29 21:24:56

标签: java linked-list indexoutofboundsexception

我正在尝试使用LinkedLists来添加最多5个长号的java应用程序。在运行结束时,我得到了这个:

  

线程“main”中的异常java.lang.IndexOutOfBoundsException:Index:   0,大小:0        在java.util.LinkedList.remove(LinkedList.java:555)的java.util.LinkedList.checkElementIndex(LinkedList.java:555)at at   Assignment1.LongNumbers.remove(LongNumbers.java:33)at   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

2 个答案:

答案 0 :(得分:0)

首先,我不认为你可以拥有一系列List<E>个对象......

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

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

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

如果您需要2个维度的列表,可以尝试使用List<List<E>>

将所有E视为Integer

答案 1 :(得分:0)

请看这里的代码:

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

您正在检查index第35个列表的第一个元素是否为空。如果是这样,您将添加它并调用remove方法。但是,如果您已经处理了第一个列表且index&#39;值为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++;
        }

最后:在未来,请构建您的代码,在这种非结构化状态下阅读它是一件非常痛苦的事情,请阅读有关如何编码的内容。