Avatar ·

When to use LinkedList instead of ArrayList in Java?

Hello! I started learning Java and encountered a problem choosing between LinkedList and ArrayList. I understand that both implement the List interface, but I can't figure out in which situations to use which. I wrote a simple test: ```java List arrayList = new ArrayList<>(); List linkedList = new LinkedList<>(); // Adding 100000 elements long start = System.nanoTime(); for (int i = 0; i < 100000; i++) { arrayList.add("Element " + i); } long end = System.nanoTime(); System.out.println("ArrayList add: " + (end - start) + " ns"); start = System.nanoTime(); for (int i = 0; i < 100000; i++) { linkedList.add("Element " + i); } end = System.nanoTime(); System.out.println("LinkedList add: " + (end - start) + " ns"); ``` ArrayList showed approximately 15-20 ms, and