How to insert an element at beginning of the ArrayList in Java?When we add elements to the ArrayList using theadd method, they are inserted at the end of the ArrayList. To insert element at the beginning or start of the ArrayList, use the overloaded add method of ArrayList which also accepts anindex parameter wherethe element needs to be inserted. Show
This method inserts the element at the specified index in the ArrayList. If there is any element exists at the specified position, it shifts the existing elements along with any subsequent elements to the right (means increases their index by 1). Java ArrayList insert an element at the beginning example
Output
As you can see from the output, we specified index as 0 in the add method to insert element at the start of the ArrayList. List automatically shifted all the existing elements by increasing their index by 1. Adding an Element to the Front of LinkedList in JavaA Linked List is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements are linked using pointers and addresses. Each element is known as a node. This article shows how to add an element to the front of LinkedList in Java. Method 1: (Using user-defined method)
Example: Java
Output
9 8 5 6
Method 2: (Using addFirst(E e) method of LinkedList) Declaration: void addFirst(Object element)Syntax: LinkedList.addFirst(e)Parameters: This function accepts a single parameter element as shown in the above syntax. The element specified by this parameter is appended at beginning of the list. Return Value: This method does not return any value. Java
Output
[YOU, ARE, HOW, HI]
Method 3: (Using offerFirst(E e)) This method also inserts the specified element at the front of the list. Declaration: Syntax: LinkedList.offerFirst(e)Parameters: Here, e is the element to add Return Value: This method returns true Example: Java
Output
[YOU, ARE, HOW, HI]
Article Tags :
Java Java Programs Technical Scripter
java-LinkedList Technical Scripter 2020 Practice Tags :
Java Java List
The Java List interface, java.util.List, represents an ordered sequence of objects. The elements contained in a Java List can be inserted, accessed, iterated and removed according to the order in which they appear internally in the Java List. The ordering of the elements is why this data structure is called a List. Each element in a Java List has an index. The first element in the List has index 0, the second element has index 1 etc. The index means "how many elements away from the beginning of the list". The first element is thus 0 elements away from the beginning of the list - because it is at the beginning of the list. You can add any Java object to a List. If the List is not typed, using Java Generics, then you can even mix objects of different types (classes) in the same List. Mixing objects of different types in the same List is not often done in practice, however. The Java List interface is a standard Java interface, and it is a subtype of the Java Collection interface, meaning List inherits from Collection. |