访问链接列表数组时出现Indexoutofbounds错误

时间:2014-03-11 15:46:50

标签: arrays indexoutofboundsexception

我在尝试编译此应用程序时遇到错误。它试图通过将数字输入到LinkedLists类型的数组中来添加任意长数。

这是我的代码,错误在“if(Lists.theLists [index] .get(0)!= null)”

import java.util.*;
/**
 *
 * @author --
 */
public class longNumbers 
{ 
 private List<Integer> [] theLists; 

 public longNumbers() //default constructor
 { 
    this.theLists = new LinkedList[11]; 
    for (int i=0; i<11; i++) 
    this.theLists[i]= new LinkedList<>(); 
 } 

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

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

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

 public static void main(String[] args) 
 {
 //Intialize Scanner Object
 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();



 //Explain purpose of program and prompt for user input
 System.out.println("The program will enter up to 10 numbers and add them up.");
 System.out.println();

 while(!userWantstoQuit && numberAt != 9)
 {
    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+1)
 {
 if(Lists.theLists[index].get(0) != null){//here is where the error occurs
    tempDigit += Lists.theLists[index].get(0);
    Lists.remove(0);                          
    }
 index++;
 }

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

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

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

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

 }//end main 

}//end class    

1 个答案:

答案 0 :(得分:0)

numberAt变量存储看到的总数。所以

while(index < numberAt+1)

应该是

while(index<numberAt)

此外,接受最多10个数字的while循环条件应为

while(!userWantsToQuit && numberAt!=10)