Am I required to set the size of a Linkedlist in Java?

Asked

Viewed 192 times

3

Since the LinkedList in Java is a double chained list, I understand that I could insert an element in any index I wanted, right? Or wrong?

For example, I create a list and use the method add(indice, elemento) to add the first item to an index with label 5.

Or it’s not, and I can only add at the beginning or end of the list and I can only put an element at index 5 if there are at least 5 of them in the list?

2 answers

6


Just take a simple test:

LinkedList<String> list = new LinkedList<>();
list.add(1, "cde");

This code launches a IndexOutOfBoundsException, and the reason is in documentation.

The constructor without arguments creates an empty list (that is, with zero size):

LinkedList<String> list = new LinkedList<>();
System.out.println(list.size()); // 0

And the method add, when you receive an index, raises the exception IndexOutOfBoundsException if the index value is less than either zero or greater than the list size:

Indexoutofboundsexception - if the index is out of range (index < 0 || index > size())

As the list size is zero, pass 1 to the method add raises the exception.


In short, we can only use indexes that are between zero and the size of the list:

LinkedList<String> list = new LinkedList<>();
list.add("abc");
list.add("def");
list.add(1,"xy");
System.out.println(list.size()); // 3
System.out.println(list); // [abc, xy, def]

list.add(3, "ultimo"); // índice é igual ao tamanho, é o mesmo que inserir no final
System.out.println(list); // [abc, xy, def, ultimo]

list.add(5,"abcde"); // IndexOutOfBoundsException
  • That’s what I meant. It doesn’t create elements in sparse indices dynamically.

6

This is very confusing, uses terms that probably only you know what it means.

A linked list is still a list, you can insert new elements into it at the beginning, at the end and in the middle, as in every list. Her difference to other lists is the cost of doing this. One of the advantages of the linked list is to be able to insert in the medium with complexity O(1), that is, it is constant, the lowest possible. Of course, it has this cost if you are in the position you want, if you are not, and often it does not have a cost of commuting there, and this cost which in many other lists is O(1), in the linked list the cost is O(n). The method you enter in the middle of the list is add(). In what his signature requires an index, this index is to be used in a get() internal. What happens if you put a non-existent index number on a get(), gives track error, right?

If the list has 5 elements you cannot insert anything beyond the fifth element, that is to say you cannot insert in index 6 (you can add that it is an operation that does not specify where you will insert, it is at the end, period). This goes for any list, it’s physical, you can’t put something on index 6 if you only have 5 elements (it goes from index 0 to 4, you can put it on 5 because it’s the same as adding). Insert is an operation to reach the desired element and add a new element there.

Note that creating a list with a number of elements (you may or may not do this) does not guarantee that you will have objects in it, objects can be null, you would have to create elements and initialize them. But if you wish you can only add elements after creating the list. The first one has to be at the end since there are no other elements. At first it works also, you can not insert in the middle for lack of elements. After there is at least one element can already insert in the middle with the add(index, value).

  • Index label = meant the index itself: the address of the element in the list.

Browser other questions tagged

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