0

How can I check if value exist in ArrayList by its index?

Java offers contains(Object) method, but no contains(Integer index) method

Thank you

2
  • How does this relate to Sharepoint? Commented Dec 20, 2009 at 15:47
  • don't know how that tag got there, removed it now, thanks for pointing it out Commented Dec 20, 2009 at 17:33

4 Answers 4

5

Not at all sure what your question is. You talk about seeing if an entry "exists" but then you talk about Java lacking a remove(Object index) method.

Regarding remove: There's the remove(int) method on the List interface implemented by ArrayList. (Java Lists are indexed by int, not by Object.)

Regarding whether an entry "exists" at a given index, if the list's size() method is greater than the index (and the index is non-negative), then there's an entry at that index:

if (index >= 0 && index < list.size()) {
    // An entry exists; hey, let's remove it
    list.remove(index);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. the post was messy because I've accidently confused "remove" and "contains"
FFR, the Java Doc specifies when you would receive and IndexOutOfBoundsException, so reversing this logic would give you valid indexes.
Simple and Amazing Solution, worked like a warm! Thanks and upvoted
3

If you have a sparse array (which can contain null values) you might want to check for this as well.

public static boolean isSet(List<?> list, int index) {
   return index >=0 && index < list.size() && list.get(index) != null;
}

Not sure why you would want the index to be Integer type but if you do you should check for null as well. Allowing a null value is the only good reason to have an Integer type.

public static boolean isSet(List<?> list, Integer index) {
   return index != null && index >=0 && index < list.size() && list.get(index) != null;
}

Comments

1

How can I check if value exist in ArrayList by its index?

Try this:

int     aIndex = ...;
boolean aIsExistByIndex = (aIndex >= 0) && (aIndex < AL.size())

Hope this help :-p

3 Comments

I don't see why this answer was voted down; the proposed expression is correct. It just lacks amplifying explanation.
Thank you, I used your code. I don't know who voted it down, but I think it was voted down because at first it missed part of the code (probably posted accidently forgot to finish it)
It was likely partly my fault as I initially forgot the escape '>' and '<' :-p.
0

Maybe you should consider using a HashMap instead of an ArrayList. There is a reason why the method exist(int index) is not implemented for ArrayList (mainly because it takes to much memory to implement an ArrayList for specifics indexes):

int i = 3; int j = 4;
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
hm.put(3, 4);
hm.containsKey(3);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.