Advertisement
1) LinkedList() |
---|
This constructor creates an empty LinkedList of a particular object type. Example - |
LinkedList<Integer> arrL= new LinkedList<Integer>();
2) LinkedList(Collection <? extends E>c) |
---|
This constructor creates a LinkedList initialized with the elements of Collection c, with a condition, that Collection c holds similar type of objects as the type declared by the LinkedList.
Example - |
LinkedList<Integer> arrL1 = new LinkedList<Integer>():
LinkedList<Integer> arrL2 = new LinkedList<Integer>(arrL1);
Methods | Description |
---|---|
boolean add(E e) | Inserts the element e at the end of an LinkedList. |
void addFirst(E e) | Inserts the element e at the beginning of the LinkedList. |
void addLast(E e) | Inserts the element e at the end of LinkedList. |
E removeFirst() | Removes the first element of the LinkedList. |
E removeLast(E e) | Removes the last element of the LinkedList. |
boolean offer(E e) | Inserts the element e at the end of the LinkedList. |
E peek() | Returns the head(first) element of the LinkedList, without removing it. |
E poll() | Returns and removes the head(first) element of the LinkedList. |
int size() | Returns the total number of elements in an ArrayList. |
void clear() | Removes all of the elements from the ArrayList |
E get(int index) | Returns the element at the specified index in an ArrayList. |
E remove(int index) | Removes an element at a specified index in an ArrayList. |
boolean remove(Object o) | Removes the first occurrence of an Object from the ArrayList, if it is present. |
Object[] toArray() | Returns an Object array containing all the elements of an ArrayList. |
Iterator |
Returns an Iterator to iterate over elements of an ArrayList. |
ListIterator |
Returns a ListIterator to iterate over elements of an ArrayList. |
Advertisement
import java.util.*;
class LinkedList1
{
public static void main(String... ar)
{
LinkedList<String> link= new LinkedList<String>();
link.add("B");
link.add("3");
link.add("B");
link.add("2");
link.addFirst("c");
link.addLast("1");
System.out.println("Linkedlist after adding objects = " + link);
System.out.println("Size of LinkedList = "+ link.size());
link.removeFirst();
System.out.println("After removing first element, LinkedList is = "+ link);
link.removeLast();
System.out.println("After removing Last element, LinkedList is = "+ link);
link.remove("2");
System.out.println("After removing 2, LinkedList is = "+ link);
LinkedList<String> link2 = new LinkedList<String>(link);
System.out.println("New LinkedList created from an existing LinkedList" + link2);
}
}
LinkedList after adding objects = [c, B, 3, B, 2, 1]
Size of LinkedList = 6
After removing first element, LinkedList is = [B, 3, B, 2, 1]
After removing Last element, LinkedList is = [B, 3, B, 2]
After removing element 2, LinkedList is = [B, 3, B]
New LinkedList created from an existing LinkedList[B, 3, B]
import java.util.*;
class LinkedList1
{
public static void main(String... ar)
{
LinkedList<Integer> link1= new LinkedList<String>();
link1.add(4);
link1.add(1);
link1.add(5);
link1.add(2);
link1.add(3);
System.out.println("LinkedList after adding objects = " + link1);
System.out.println("Size of LinkedList = "+ link1.size());
System.out.println("Creating a new LinkedList created from an existing one");
LinkedList<Integer> link2= new LinkedList<Integer>(link1);
System.out.println("Contents of new LinkedList : " + link2);
System.out.println("Size of new LinkedList = "+ link2.size());
}
}
LinkedList after adding objects = [4, 1, 5, 2, 3]
Size of LinkedList = 5
Creating a new LinkedList created from an existing LinkedList
Contents of new LinkedList : [4, 1, 5, 2, 3]
Size of new LinkedList = 5
import java.util.*;
class LinkedList1
{
public static void main(String... ar)
{
LinkedList<Integer> link1= new LinkedList<Integer>();
link1.add(40);
link1.add(10);
link1.add(50);
link1.add(20);
link1.add(30);
System.out.println("LinkedList after adding objects = " + link1);
System.out.println("Size of LinkedList = "+ link1.size());
//Converting an LinkedList to an Object array.
Object[] a = link1.toArray();
System.out.print("Contents of an Object Array created from LinkedList - ");
/Iterating over the elements of Object array
for(Object element : a)
System.out.print(element + " " );
}
}
LinkedList after adding objects = [40, 10, 50, 20, 30]
Size of LinkedList = 5
Contents of an Object Array after created from LinkedList - 40 10 50 20 30
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement