Results 1 to 4 of 4
  1. #1
    jameshl2008 is offline Member
    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Default Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0

    Really confused.. could someone give me an idiot's explanation? Other people weren't able to help and honestly, it seems to me that this code should work fine and it is kind of pissing me off, why can't i set result.num with the int three variable? i have the same result.num.set(0,three) line in another method and it works perfectly

    I'm not really sure why I keep getting this error message. I'm trying to add int three to result. I don't understand why it doesn't let me do this, it seems result would be empty when I try to put three into index size1-1.

    Error message: (Lines 452, 423, 11 are marked)

    Java Code:
    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.set(Unknown Source)
    at BigInt.singleDigitMultiply(BigInt.java:452)
    at BigInt.multiply(BigInt.java:423)
    at Dem.main(Dem.java:11)
    Portion of BigInt class:
    Java Code:
    public BigInt multiply(BigInt B2) {
    BigInt result = new BigInt();
    BigInt B1 = this;   
    result = singleDigitMultiply(B1,B2); Line 423
        return result;
    
    }
    
    private BigInt singleDigitMultiply(BigInt B1, BigInt B2) {
        int size1 = B1.num.size(), size2 = B2.num.size();
        BigInt result = new BigInt();
        //result.num = new ArrayList (Collections.nCopies( size1+1, null ));
        boolean carryover = false;
        int one, two, three, four, five, carry;
        for (int i = size1-1; i >= 0; i--) {
    
             one = B1.num.get(i);
             two = B2.num.get(0); 
    
             int prod = B1.num.get(i) * B2.num.get(0);
             int prodInt = prod / 10;
             if (prod > 9) carryover = true;
    
             if (i == size1-1) {
    
             if (carryover) {
             three = prod % (prodInt * 10); 
             }
             else {
                three = prod;
             }
    
        System.out.println( result.num.set(size1-1,three));          Line 452
             }
    
        //   else {
            //   four = B1.num.get(i+1) * two;
        //       carry = four % (four - prodInt);
        //       if (four > 9) {
        //   
        //       five = prod + carry;
        //       three = five % (prodInt * 10);
        //       }
        //       else {
        //           three = prod;
        //       }
            //   result.num.add(i,three);
        //   }
    
    
    
    
        }
    
        return result;
    }
    Main class:

    Java Code:
    public class Dem {
    public static void main(String[] args) {
    BigInt B1 = new BigInt("55");
    BigInt B2 = new BigInt("1");
    BigInt D = new BigInt();
    int u = 8;
    int j = 10;
    
    //BigInt J = new BigInt("u");
    D = B1.multiply(B2);           Line 11
    System.out.println(u/j);
    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.set(Unknown Source)
    at BigInt.singleDigitMultiply(BigInt.java:452)
    The ArrayList is empty(size=0) so an index of 1 is out of bounds.

    Where does the code put any items into the ArrayList? Are there any items in the list at line 452?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    jameshl2008 is offline Member
    Join Date
    Sep 2013
    Posts
    5
    Rep Power
    0

    Default Re: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0

    Quote Originally Posted by Norm View Post
    The ArrayList is empty(size=0) so an index of 1 is out of bounds.

    Where does the code put any items into the ArrayList? Are there any items in the list at line 452?
    Thanks, I am trying to put the item int three into the arraylist result even though there is nothing in the result arraylist to begin with.

    I've tried filling it with null objects and making results value 0, but none of these have allowed me to put int three into the arraylist. When I tried these previous two options the program ran obviously but the set method in the arraylist class did not override my respective element (int) over the previous null objects/"0" value placed in result.num.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0

    am trying to put the item int three into the arraylist result
    Instead of using set() did you try any other method to put an item into the arraylist?
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: 06-20-2012, 01:18 AM
  2. Replies: 5
    Last Post: 12-21-2011, 07:44 PM
  3. Replies: 14
    Last Post: 04-08-2011, 07:01 PM
  4. Replies: 5
    Last Post: 06-22-2010, 04:51 PM
  5. Replies: 1
    Last Post: 11-20-2009, 08:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •