Exception when working with List collections

  • 3
By the ninth level, we've already got some idea of List collections, and exceptions. Let's bring this knowledge together: catch exceptions when executing the following code: ArrayList<String> list = new ArrayList<String>(); String s = list.get(18); Catch and display the exception on the screen, indicating its type.
You can't complete this task, because you're not signed in.
Comments (3)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Nirali Rawal
Level 15, Hopewell, United States
1 October 2019, 15:22
Why exactly does this have an IndexOutofBoundsException? I thought in a list we can add as many values as possible, hence I'm not sure why the IndexOutofBoundException
Roman
Level 41
2 October 2019, 06:04
Because after initialization
ArrayList<String> list = new ArrayList();
list doesn't have element with index == 18
Isma
Level 31, Madrid, Spain
2 December 2020, 12:19
I'm not sure but I guess It's because
ArrayList list = new ArrayList();
creates an ArrayList with default size 10 and the index 18 is out of that array. Can anyone confirm this?