Search code examples
javaarraysbooleanindexoutofboundsexception

How do I see if an array index I'm accessing is out of range of the array?


Is there anyway I can use some boolean expression to see if what I want to access is out of bounds?


Solution

  • if you have array like this(int[] a = new in[10]) you can test it like

    if(index < a.length)
    

    or if you have ArrayList<Integer> b = new ArrayList<Integer>(); you can test it like

    if( index < b.size())