-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

2
  • 3
    Please do not post images of your code. Copy-paste the code here, using the proper formatting Commented Sep 10, 2019 at 1: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) Commented Sep 10, 2019 at 1:41

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

1 Comment

I also suggest you add the following reference: stackoverflow.com/questions/24841172/…
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.