InterviewSolution
Saved Bookmarks
| 1. |
ArrayList vs LinkedList in Java |
|
Answer» ArrayList and LinkedList are very important lists in Java. While the ArrayList implements a DYNAMIC array, the LinkedList implements a doubly linked list. Some of the differences between Arraylist and LinkedList are given as follows:
A program that demonstrates ArrayList in Java is given as follows: import java.util.ArrayList; public class ArrayListDemo { public static void main(String[] args) { ArrayList<String> al= new ArrayList<String>(); al.add("Apple"); al.add("Orange"); al.add("Guava"); al.add("MANGO"); al.add("Peach"); System.out.println(al); al.remove(2); System.out.println(al); if (al.contains("Apple")) System.out.println("Apple found in ArrayList"); else System.out.println("Apple not found in ArrayList"); } }The output of the above program is as follows: [Apple, Orange, Guava, Mango, Peach] [Apple, Orange, Mango, Peach] Apple found in ArrayListA program that demonstrates LinkedList in Java is given as follows: import java.util.LinkedList; public class LinkedListDemo { public static void main(String[] args) { LinkedList l = new LinkedList(); l.add("Apple"); l.add("Orange"); l.add("Guava"); l.add("Mango"); l.add("Peach"); System.out.println(l); l.remove(0); System.out.println(l); if (l.contains("Apple")) System.out.println("Apple found in LinkedList"); else System.out.println("Apple not found in LinkedList"); } }The output of the above program is as follows: [Apple, Orange, Guava, Mango, Peach] [Orange, Guava, Mango, Peach] Apple not found in LinkedList |
|