Basic doubt in Linkedlist - Java

Asked

Viewed 77 times

2

If I have a Linkedlist called List that has size equal to 0, I can add an element at position 4 of it, for example?

1 answer

5

Not.

The method add has the signature

public void add(int index, E element)

where index is the index where the element will be inserted and element is the element to be inserted.

Such a method casts the exception IndexOutOfBoundsException if the index is outside the range (index < 0 || index > size()).

In your case, 4 > 0, resulting in exception.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.