Bifalgorithm post · Computer science one · Basic Usage

How does indexing work in Java arrays?

How does indexing work in Java arrays? (Computer science one · Basic Usage) — "Once an array has been created, its elements can be accessed by indexing them. J…

Ideas in this flowchart

  • Accès aux éléments d’un tableau Java par indexation
  • Les éléments d’un tableau sont accessibles à l’aide d’un index entier.
  • Java utilise l’indexation à partir de zéro : le premier élément est à l’index 0, le deuxième à l’index 1, etc.
  • La notation entre crochets fournit l’index de l’élément, par exemple arr[0].
  • Une fois indexé, un élément de tableau peut être traité comme une variable normale.
  • Un élément indexé peut être utilisé avec l’affectation, la comparaison ou d’autres opérateurs.
  • Un index représente en réalité un décalage en octets depuis le premier élément.
  • Le compilateur et le système calculent la position de l’élément à partir de la taille de chaque élément.
  • Des index hors limites comme arr[-1] ou arr[5] tentent d’accéder à des éléments qui ne font pas partie du tableau.
  • Un accès hors limites provoque une IndexOutOfBoundsException, exception non vérifiée de type RuntimeException.
  • Cette exception peut être interceptée et traitée si nécessaire.
  • Pour éviter l’exception, le code doit rester dans les limites du tableau.
  • La propriété length fournit la taille du tableau.
  • L’opérateur point permet d’accéder à cette propriété : arr.length vaut 5 pour l’exemple donné.
Feed/

How does indexing work in Java arrays?

No cover

Channel

Computer science one

1 page

"Once an array has been created, its elements can be accessed by indexing them. Java uses the standard 0-indexing scheme so the first element is at index 0, the second at index 1, etc. Indexing an element involves using the square bracket notation and providing an integer index. Once indexed, an array element can be treated as a normal variable and can be used with other operators such as the assignment operator or comparison operators.\n\narr[0] = 42;\n\nif(arr[4] < 0) {\n\nSystem.out.println(\"negative!\");\n\n}\n\nSystem.out.println(\"arr[1] = \" + arr[1]);\n\nRecall that an index is actually an offset. The compiler and system know exactly how many bytes each element takes and so an index i calculates exactly how many bytes from the first element the i-th element is located at. Consequently it is possible to index elements that are beyond the range of the array. For example, arr[-1] or arr[5] would attempt to access an element immediately before the first element and immediately after the last element. Obviously, these elements are not part of the array. If you attempt to access an element outside the bounds of an array, the JVM will throw an IndexOutOfBoundsException which is an unchecked RuntimeException that you can catch and handle if you choose. To prevent such an exception you can write code that does not exceed the bounds of the array. Java arrays have a special length property that gives you the size of the array. You can access the property using the dot operator, so arr.length would give the value 5 for the example above."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.