ArrayIndexOutOfBoundsException大数字添加程序

时间:2012-03-24 23:27:27

标签: java

我正在研究大量的添加程序(不使用biginteger类)。我以为我理解了这一点,但出于某种原因,我的add方法得到了ArrayIndexOutOfBoundsException

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20
    at LargeInteger.add(LargeInteger.java:50)
    at testLargeInteger.main(testLargeInteger.java:32)

主:

import java.util.Scanner;

public class testLargeInteger
{



public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);
        String string1;
        String string2;
        int exp =0;


        System.out.print("Enter the first integer: ");
        //Store up the input string “string1” entered by the user from the keyboard.
        string1 = input.next(); 

        LargeInteger firstInt = new LargeInteger(string1);

        System.out.print("Enter the second integer: ");
        string2 = input.next(); 
        //Store up the input string “string2” entered by the user from the keyboard.
        LargeInteger secondInt = new LargeInteger(string2);

        System.out.print("Enter the exponential integer: ");
        //Store up the input integer “exp” entered by the user from the keyboard.
        exp = input.nextInt(); 


        LargeInteger sum = firstInt.add(secondInt);

        System.out.printf ("First integer: %s \n", firstInt.display());
        System.out.println("Second integer: " + secondInt.display());
        System.out.println(" Exponent: " + exp);

        System.out.printf (" Sum = %s \n", sum.display());

    }
}

LargeInteger类:

public class LargeInteger {


    private int[] intArray;


    //convert the strings to array
    public LargeInteger(String s) { 
         intArray = new int[s.length()];
        for (int i = 0; i < s.length(); i++) {
            intArray[i] = Character.digit(s.charAt(i), 10); // in base 10
        }
    }

    public LargeInteger( int[] array ) { 
         intArray = array;
    }

    //display the strings
    public String display() {           
          String result="";

          for (int i = 0; i < intArray.length; i++) {     
            result += intArray[i];
          }
          return result.toString();
        }   

    //get first array
    public int[] getIntArray() {
           return intArray;
        }

    public LargeInteger add(LargeInteger secondInt){

        int[] otherValues = secondInt.getIntArray();

        int maxIterations = Math.min(intArray.length, otherValues.length);
        int currentResult; //to store result 
        int[] resultArray = new int[Math.max(intArray.length, otherValues.length) + 1];

        int needToAdd = 0; //to store result should be added next step

        for(int i = 0; i < maxIterations; i++) {
            currentResult = intArray[i] + otherValues[i];
            resultArray[i] = currentResult % 10 + needToAdd; //if more than 9 its correct answer
            needToAdd = currentResult / 10; //this is what you need to add on next step
        }

        resultArray[Math.max(intArray.length, otherValues.length) + 1] = needToAdd;

        return new LargeInteger( resultArray );

    }

}

2 个答案:

答案 0 :(得分:3)

在此声明数组的长度:

int[] resultArray = new int[Math.max(intArray.length, otherValues.length) + 1];

在这里你使用相同的长度访问它:

 resultArray[Math.max(intArray.length, otherValues.length) + 1] = needToAdd;

在Java(通常是计算机语言)中,数组索引从0开始并以长度结束 - 1.因此,如果声明一个数组长10个元素,则索引为0-9。因此,您必须减去一个:

resultArray[Math.max(intArray.length, otherValues.length)] = needToAdd;

答案 1 :(得分:2)

resultArray[Math.max(intArray.length, otherValues.length) + 1] = needToAdd;
java中的

数组从0开始,你分配的空间与元素大小完全相同:

int[] resultArray = new int[Math.max(intArray.length, otherValues.length) + 1];

所以,你得到一个索引,因为你从数组中访问一个元素。