tags:

views:

1043

answers:

6

Hi. I'm using ArrayList and i add data in specific indexes, how can i check if a specific index exists ?

should i just get() and check the value or wait for an exception or is there another way ?

update

thank you for your answers but i add stuff in specific indexes

once i will add at index 8, other times at index 2. the length of the list will not show me the available indexes.

+10  A: 

The method arrayList.size() returns the number of items in the list - so if the index is greater than or equal to the size(), it doesn't exist.

Amarghosh
That should be "greater or equal to the `size()`" since it is a zero-based index.
McDowell
@McDowell - oops, fixed it. thanks.
Amarghosh
Also worth mentioning that to make this atomic you should probably perform the size() check and corresponding conditional index based look-up whilst locking the list.
Adamski
please note that i mark this answer at the correct is because the owner (Amarghosh) as answered my question in a comment to my question. HashTable will suite my needs much better.
ufk
+1  A: 

If your index is less than the size of your list then it does exist, possibly with null value. If index is bigger then you may call ensureCapacity() to be able to use that index.

If you want to check if a value at your index is null or not, call get()

Dmitry
Calling ensureCapacity(int) won't increase the size of the List, only the capacity; i.e. "potential size" so out of bounds index look-ups would still fail.
Adamski
Also, why call ensureCapacity(int) at all? It could be an incredibly expensive operation if for example the list's current size is 5 and you want to determine the value of item#: 100,000,000.
Adamski
I meant that indices less than size() always exist, those which are >= size() don't and one can't use them (== call set()) until list becomes big enough.Calling ensureCapacity is not enough indeed, one needs to change the size by adding elements.
Dmitry
+2  A: 

You can check the size of an ArrayList using the size() method. This will return the maximum index +1

jwoolard
A: 

You could check for the size of the array.

package sojava;
import java.util.ArrayList;

public class Main {
    public static Object get(ArrayList list, int index) {
        if (list.size() > index) { return list.get(index); }
        return null;
    }

    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.add(""); list.add(""); list.add("");        
        System.out.println(get(list, 4));
        // prints 'null'
    }
}
The MYYN
A: 

This is what you need ...

public boolean indexExists(final List list, final int index) {
    return index >= 0 && index <= list.size();
}

Why not use an plain old array? Indexed access to a List is a code smell I think.

Paul McKenzie
+1  A: 

Regarding your update (which probably should be another question). You should use an array of these objects instead an ArrayList, so you can simply check the value for null:

Object[] array = new Object[MAX_ENTRIES];
..
if ( array[ 8 ] == null ) {
   // not available
}
else {
   // do something
}

Best-Practice

If you don't have hundred of entries in your array you should consider organizing it as a class to get rid of the magic numbers 3,8 etc.

Control flow using exception is bad practice.

stacker