When to use LinkedList instead of ArrayList in Java?
Hello everyone! I'm new to Java and trying to understand collections. I've read that there are ArrayList and LinkedList, but I can't figure out when to use which one.\n\nFor example, I need to store a list of tasks in a planning application. I often add new tasks to the end of the list and sometimes delete from the middle. Here's an example of my code:\n\n```java\nList tasks = new ArrayList<>();\ntasks.add("Buy groceries");\ntasks.add("Do homework");\ntasks.add("Call a friend");\ntasks.add("Go to the gym");\n\n// Sometimes I delete a task from the middle\ntasks.remove(1); // Remove "Do homework"\n```\n\nArrayList works fine, but I've heard that deleting from the middle is a slow operation. LinkedList seems to be better for frequent insertions and deletions. But it has more overhead on pa
No answers yet.
Log in to leave an answer
Blogs