title: Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 10, Size: 0. JAVA
body: <p>I want to use an <code>ArrayList</code> to represent the priority queue. So I want to add item on specific position of the <code>ArrayList</code>. But when I run it, system told me <code>Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 10, Size: 0.</code></p>
  
  <pre><code>public class PriorityQueue 
  {
  public ArrayList&lt;String&gt; Queue=new ArrayList&lt;&gt;();
  
  public void enqueu(String s, int p)
  {
      Queue.add(p,s);
  }
  
  public void dequeu()
  {
      String temp=Queue.get(Queue.size()-1);
      Queue.remove(temp);
  }
  public void print()
  {
      String[] print=new String[Queue.size()];
      print=Queue.toArray(print);
      for(int i=0;i&lt;Queue.size();i++)
      {
          System.out.println(print[i]);
      }
  
  
  }
  
   public static void main(String[] args)
   {
       PriorityQueue test= new PriorityQueue();
       test.enqueu("x",10);
       test.enqueu("Y",1);
       test.enqueu("Z",3);
  
       test.print();
   }}
  </code></pre>