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 Arraylist.add(id,obj) or Arraylist.set(id,obj) . I use the following test if(Arraylist.get(t.imgIdx) == null) , but it throws me Exception out of bounds all the time. How can/should I test it?
public static int GiveBackAverageID(Vector<DMatch> lista){
ArrayList<CMatch> workingList = new ArrayList<CMatch>();
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);
}
}
...
thanks
Csabi
If you want to test if an object is at a position, use
IndexOf(). This method returns -1 if the object is not in the list.UPDATE
On your new piece of code:
Or what you could also do, is generate more capacity in your
workingList:As a better alternative, I would use a
HashTable<int,DMatch>instead.