Csabi
Csabi

Reputation: 3043

how can I test in Java if an Object exists at specific index?

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

Upvotes: 0

Views: 7032

Answers (5)

Marnix
Marnix

Reputation: 6547

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:

public static int GiveBackAverageID(Vector<DMatch> lista){

    ArrayList<CMatch> workingList = new ArrayList<CMatch>();

    for (DMatch t : lista){
        if(t.imgIdx >= workingList.size() || t.imgIdx < 0)
        {
            // do something with wrong indices.
        }
        else
        {
            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);
            }
        }
    }
}

Or what you could also do, is generate more capacity in your workingList:

public static int GiveBackAverageID(Vector<DMatch> lista){

    // Creating more capacity in the constructor!
    ArrayList<CMatch> workingList = new ArrayList<CMatch>(lista.size());

    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);
        }
    }
}

As a better alternative, I would use a HashTable<int,DMatch> instead.

Upvotes: 3

Jayy
Jayy

Reputation: 2436

I think the problem with your code is that t.imgIdx value is sometimes greater than the array size.

But when you access the element (which is supposed to be null) like the code you did if(workingList.get(t.imgIdx) == null), your if condition will return boolean value provided that the parameter passed to get() is less than the size of the array.

You can try the below example and pass different parameter values to get() method.

public static void main(String[] args) {

    ArrayList al = new ArrayList();
    al.add(0, "5");
    al.add(1, "10");
    al.add(2, "15");
    al.add(3, "20");

    al.set(1, null);//setting the element at index 1 to NULL.
    //al.remove(1);//removes the element in list so that the next element's index decreases by 1. 

    if(al.get(1) == null){
        System.out.println("Nothing here..");//this line executes
    }
    else
        System.out.println(al.get(1));

}

Upvotes: 1

pgbsl
pgbsl

Reputation: 222

YOu need to check the size of the list before you try to insert. If the index is less than the list size, then you should be ok to check to see if it is null, otherwise, you always want to add a new element

Upvotes: 1

Tobias
Tobias

Reputation: 7380

Catch the Exception and handle it properly, it says that there is no element with this index.

Upvotes: 0

NimChimpsky
NimChimpsky

Reputation: 47290

use set to replace at a specific index, and add to add object at the end, and add with index value to insert object at position and move other elements to right (adding one to their index).

Out of bounds exception means your index value is, probably, to large; or the arraylist has not been populated as you expect. Post the full exception/code for a complete answer.

Upvotes: 1

Related Questions