title: how can I test in Java if an Object exists at specific index?
body: <p>I use Arraylist and inserting Objects to specific index.(for example is element at index of 0 and 2 but it is not at at index of 1)And I want to know if I should use <code>Arraylist.add(id,obj)</code> or <code>Arraylist.set(id,obj)</code> . I use the following test <code>if(Arraylist.get(t.imgIdx) == null)</code> , but it throws me Exception out of bounds all the time. How can/should I test it?</p>
  
  <pre><code>public static int GiveBackAverageID(Vector&lt;DMatch&gt; lista){
  
          ArrayList&lt;CMatch&gt; workingList = new ArrayList&lt;CMatch&gt;();
  
          for (DMatch t : lista){
              if(workingList.get(t.imgIdx) == null){
                  workingList.add(t.imgIdx, new CMatch(t.imgIdx,t.distance,1));
              }else{
                  CMatch pom = workingList.get(t.imgIdx);
                  pom.setSummaDist(pom.getSummaDist()+t.distance);
                  pom.setCount(pom.getCount()+1);
                  workingList.set(t.imgIdx, pom);
              }
          }
  ...
  </code></pre>
  
  <p>thanks
  Csabi</p>