Created
July 6, 2022 12:24
-
-
Save manjaykumardp/70988a759dba1108c5813e9c187ec56d to your computer and use it in GitHub Desktop.
program to how implement arrayList
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.LinkedList; | |
import java.util.List; | |
public class LinkList { | |
public static void main(String[] args) { | |
// Creating a LinkedList | |
LinkedList<String> friends = new LinkedList<>(); | |
// Adding new elements to the end of the LinkedList using add() method. | |
friends.add("Rajeev"); | |
friends.add("John"); | |
friends.add("David"); | |
friends.add("Chris"); | |
System.out.println("Initial LinkedList : " + friends); | |
// Adding an element at the specified position in the LinkedList | |
friends.add(3, "Lisa"); | |
System.out.println("After add(3, \"Lisa\") : " + friends); | |
// Adding an element at the beginning of the LinkedList | |
friends.addFirst("Steve"); | |
System.out.println("After addFirst(\"Steve\") : " + friends); | |
// Adding an element at the end of the LinkedList (This method is equivalent to the add() method) | |
friends.addLast("Jennifer"); | |
System.out.println("After addLast(\"Jennifer\") : " + friends); | |
// Adding all the elements from an existing collection to the end of the LinkedList | |
List<String> familyFriends = new ArrayList<>(); | |
familyFriends.add("Jesse"); | |
familyFriends.add("Walt"); | |
friends.addAll(familyFriends); | |
System.out.println("After addAll(familyFriends) : " + friends); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment