Results 1 to 3 of 3
  1. #1
    GaBuG is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default ArrayList size while adding or setting

    Hi there,

    I'm having trouble working with an ArrayList. The thing is I declare the array and want to add() (also tried with set()) an element in a position that is not the first one (thus index != 0). I keep getting this message:


    Java Code:
    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size:0

    Reading the doc I see that the default constructor sets an initial size of 10. Also tried with the constructor that receives an int as initial size. So if the size is initiated as 10, why does the compiler complain about the index? Here is some code, maybe I am missing something obvious...

    Java Code:
    public class Polynom {
    	ArrayList<Pair> terms;
    	
    	Polynom() {
    		terms = new ArrayList<Pair>();
    	}
    	
    	Polynom(int size) {
    		terms = new ArrayList<Pair>(size);
    	}
    	
    	Polynom(String fileName) {
    		terms = new ArrayList<Pair>();
    		readPolynom(fileName, terms);
    	}
    	
    	
    	public static void main(String args[]) {
    		Polynom p1 = new Polynom("p1.txt");
    		System.out.println("Polynom 1: " + p1);
    		Polynom p2 = new Polynom("p2.txt");
    		System.out.println("Polynom 2: " + p2);
    		Polynom addition = p1.plus(p2);
    		System.out.println("Addition : " + addition);
    		Polynom substraction = p1.minus(p2);
    		System.out.println("Substraction : " + substraction);
    		Polynom multiplication = p1.times(p2);
    		System.out.println("Multiplication : " + multiplication);
    	}
    
    	Polynom times(Polynom target) {
    		Polynom tempPoly = new Polynom();
    		int callerDeg = this.terms.size() - 1;
    		int targetDeg = target.terms.size() - 1;
    		int i, j, k, tempDeg;
    		double tempCoef;
    		ArrayList<Polynom> halfResult = new ArrayList<Polynom>();
    		
    		for (i = callerDeg; i >= 0; i--) {
    			tempPoly = new Polynom(Math.max(callerDeg, targetDeg) + 1);
    			System.out.println(tempPoly.terms.size());
    			for (j = targetDeg, k = i; j >= 0; j--) {
    				tempCoef = this.terms.get(i).coef * target.terms.get(j).coef;
    				tempDeg = this.terms.get(i).deg + target.terms.get(j).deg;
    /* THE LINE OF THE ERROR -> */  tempPoly.terms.set(k, new Pair(tempCoef, tempDeg));
    				k--;
    			}
    			halfResult.add(tempPoly);
    		}
    /*
     *		for (Polynom tempAddition : halfResult) {
     *			System.out.println("****" + tempAddition);
     *		}
     *		return tempPoly;
     */	}
    }

    So as you can see I want to implement polynomial multiplication. The code is not finished at the commented part, I wanted to see the code working first.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    initial capacity is only for the internal array, but don`t set the size of the arraylist.
    set(int index, E element) invokes the RangeCheck method, and that method checks if the index is in the range (index < size - --> size = the number of elements it contains, not the initial capacity !!!)
    Last edited by eRaaaa; 01-02-2011 at 07:21 PM.

  3. #3
    GaBuG is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default

    Hey thanks for your answer,

    Then what I understand is that to make it work I should set all the elements with an empty value so RangeCheck will be ok with it... trying that...

Similar Threads

  1. Adding books into arrayList
    By mwenchong in forum New To Java
    Replies: 9
    Last Post: 12-15-2010, 03:10 PM
  2. ArrayList pointer size java
    By senorbum in forum Advanced Java
    Replies: 6
    Last Post: 05-20-2010, 10:44 AM
  3. Setting frame size to the size of an image
    By Yoruichi in forum AWT / Swing
    Replies: 5
    Last Post: 04-22-2009, 05:37 PM
  4. setting applet's size
    By nikita in forum Java Applets
    Replies: 9
    Last Post: 10-01-2008, 05:41 AM
  5. Limiting size of ArrayList
    By ravian in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 07:37 AM

Posting Permissions

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