add(int, K): This method is available in java.util.LinkedList class of Java.
Syntax:
void java.util.LinkedList.add(int index, Integer element)
This method takes two arguments. This method inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
Parameters: Two parameters are required for this method.
index: index at which the specified element is to be inserted.
element: the element to be inserted.
Throws:
IndexOutOfBoundsException - if the index is out of range(index < 0 || index > size())
Approach 1: When no exception
Java
import java.util.LinkedList;public class LinkedListadd2 {public static void main(String[] args) {LinkedList<Integer> linkedList = new LinkedList<>();linkedList.add(1);linkedList.add(2);int index = 1;Integer element = 25;linkedList.add(index, element);System.out.println(linkedList);}}
Output:
[1, 25, 2]
Approach 2: IndexOutOfBoundsException
Java
import java.util.LinkedList;public class LinkedListadd2 {public static void main(String[] args) {LinkedList<Integer> linkedList = new LinkedList<>();linkedList.add(1);linkedList.add(2);int index = 3;Integer element = 25;linkedList.add(index, element);System.out.println(linkedList);}}
Output:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 2 at java.base/java.util.LinkedList.checkPositionIndex(LinkedList.java:564) at java.base/java.util.LinkedList.add(LinkedList.java:511)
Some other methods of LinkedList
add(K): This method appends the specified element to the end of this list.
addAll(Collection): This method appends all of the elements in the specified collection to the end of this list.
addAll(int, Collection): This method inserts all of the elements in the specified collection into this list, starting at the specified position.
addFirst(K): This method inserts the specified element at the beginning of this list.
addLast(K e): This method appends the specified element to the end of this list.
clear(): This method removes all of the elements from this list.
clone(): This method returns a shallow copy of this LinkedList.
contains(Object): This method returns true if this list contains the specified
containsAll(Collection): This method returns true if this collection contains all of the elements in the specified collection.
descendingIterator(): This method returns an iterator over the elements in this deque in reverse sequential order.
element(): This method retrieves but does not remove, the head (first element) of this list.
equals(Object): his method compares the specified object with this list for equality.
forEach(Consumer): This method performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.
get(int): This method returns the element at the specified position in this list.
getFirst(): This method returns the first element in this list.
getLast(): This method returns the last element in this list.
hashCode(): This method returns the hash code value for this list.
indexOf(Object): This method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
isEmpty(): This method returns true if this collection contains no elements.
iterator(): This method returns an iterator over the elements in this list.
lastIndexOf(Object): This method returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
listIterator(): This method returns a list iterator over the elements in this list.
listIterator(int): This method returns a list-iterator of the elements in this list starting at the specified position in the list.
offer(K): This method adds the specified element as the tail (last element) of this list.
offerFirst(K): This method inserts the specified element at the front of this list.
offerLast(K): This method inserts the specified element at the end of this list.
parallelStream(): This method returns a possibly parallel Stream with this collection as its source.
peek(): This method retrieves but does not remove, the head (first element) of this list.
peekFirst(): This method retrieves, but does not remove, the first element of this list, or returns null if this list is empty.
peekLast(): This method retrieves, but does not remove, the last element of this list, or returns null if this list is empty.
poll(): This method retrieves and removes the head (first element) of this list.
pollFirst(): This method retrieves and removes the first element of this list, or returns null if this list is empty.
pollLast(): This method retrieves and removes the last element of this list, or returns null if this list is empty.
pop(): This method pops an element from the stack represented by this list.
push(K e): This method pushes an element onto the stack represented by this list.
remove(): This method retrieves and removes the head (first element) of this list.
remove(int): This method removes the element at the specified position in this list.
remove(Object): This method removes the first occurrence of the specified element from this list if it is present.
removeAll(Collection): This method removes all of this collection's elements that are also contained in the specified collection.
removeFirst(): This method removes and returns the first element from this list.
removeFirstOccurrence(Object): This method removes the first occurrence of the specified element in this list.
removeIf(Predicate): This method removes all of the elements of this collection that satisfy the given predicate.
removeLast(): This method removes and returns the last element from this list.
removeLastOccurrence(Object): This method removes the last occurrence of the specified element in this list.
replaceAll(UnaryOperator): This method replaces each element of this list with the result of applying the operator to that element.
retainAll(Collection): This method retains only the elements in this collection that are contained in the specified collection.
set(int, K): This method replaces the element at the specified position in this list with the specified element.
size(): This method returns the number of elements in this list.
sort(Comparator): This method sorts this list according to the order induced by the specified Comparator.
spliterator(): This method creates a late-binding and fail-fast Spliterator over the elements in this list.
stream(): This method returns a sequential Stream with this collection as its source.
subList(int, int): This method returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
toArray(): This method returns an array containing all of the elements in this list in proper sequence (from the first to the last element).
toArray(IntFunction): This method returns an array containing all of the elements in this collection, using the provided generator function to allocate the returned array.
toArray(K[]): This method returns an array containing all of the elements in this list in proper sequence (from first to the last element); the runtime type of the returned array is that of the specified array.
toString(): This method returns a string representation of this collection.
No comments:
Post a Comment