Question

I can't create an ArrayList of size 3. Any hits to help me solve this problem are appreciated. The Output for the print statement are "capacity 3" and array size 0.

This is what I use for the Junit test
BoundedDeque< Integer > arr = new Deque< Integer >(3);

public class Deque<E> implements BoundedDeque<E>
{

   protected int capacity, size, back, front;
   ArrayList<E> arraylist;


   public Deque(int capacity)
   { 

     System.out.println("capacity"+ " "+capacity);
     this.capacity = capacity;
     arraylist= new ArrayList<E>(capacity);
     front = 0;
     back = capacity-1;
     System.out.println("array size"+ " "+arraylist.size());
    }

.........
Was it helpful?

Solution

You're confusing two things in the ArrayList, size and capacity. Size is how many elements are in your ArrayList, and since you never add anything to your ArrayList, 0 is the perfectly correct result. Capacity is just the initial capacity of the underlying array and is only relevant for performance purposes.

For more information you should see the JavaDoc: ArrayList

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top