-2

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 at arraydekla.main(arraydekla.java:19) C:\Users\acer\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)

My code:

My code

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • 3
    Please do **not** post images of your code. Copy-paste the code here, using the proper formatting – Frontear Sep 10 '19 at 01:40
  • 2
    `3` is not a valid index as you've made an array of size two by three, the max index of your inner arrays will be `2` (valid indexes are: `0, 1, 2` for an array of size 3) – Nick Parsons Sep 10 '19 at 01:41

2 Answers2

1

Arrays in Java are 0-based.

This means that you start counting indexes from 0. Thus, an array of size 2 will contain elements at indexes number 0, and 1. An array of size 3 will contain elements at indexes 0, 1, and 2.

In your code, you are accessing the element at [0,3] on line 19, and [2, 0] on line 23. Lines 23 and 27 contain the same type of error.

For more info:

https://www.w3schools.com/java/java_arrays.asp

Why is array indexing in Java start with 0?

  • I also suggest you add the following reference: https://stackoverflow.com/questions/24841172/why-is-array-indexing-in-java-start-with-0 – azbarcea Sep 10 '19 at 01:59
0

ArrayIndexOutOfBoundsException is thrown to indicate that an array is accessed with an illegal index. Index of an array always starts with zero.

As the array is of size [2][3], we can only access the array elements [0][0], [0][1], [0][2], [1][0], [1][1], [1][2].

We cannot access [2][3], [1][3] as the index is greater than the size of the array.