You are here

Array Accesses

15 January, 2016 - 09:02
Available under Creative Commons-ShareAlike 4.0 International License. Download for free at http://cnx.org/contents/402b20ad-c01f-45f1-9743-05eadb1f710e@37.6
  • Indices for arrays must be int values that are greater than or equal to 0 and less than the length of the array. Remember that computer scientists always count starting at zero, not one!
  • All array accesses are checked at run time: An attempt to use an index that is less than zero or greater than or equal to the length of the array causes an IndexOutOfBoundsException to be thrown.
  • Array elements can be used on either side of an equals sign:
    • myArray[i] = aValue;
    • someValue = myArray[j];
  • Accessing elements of an array is fast and the time to access any element is independent of where it is in the array.
  • Inserting elements into an array is very slow because all the other elements following the insertion point have to be moved to make room, if that is even possible.