Found 4338 Articles for Java 8

What does the method removeLast() do in java?

Abhinaya
Updated on 30-Jul-2019 22:30:21

61 Views

The removeLast() method of the java.util.LinkedList class removes and returns the last element of this list.Example:import java.util.*; public class LinkedListDemo {    public static void main(String[] args) {       LinkedList list = new LinkedList();       list.add("Hello");       list.add(2);       list.add("Chocolate");       list.add("10");       System.out.println("LinkedList:" + list);       System.out.println("Last element:" + list.removeLast());       System.out.println("LinkedList:" + list);    } }Output:LinkedList:[Hello, 2, Chocolate, 10] Last element:10 LinkedList:[Hello, 2, Chocolate]

What does the method removeFirst() do in java?

Govinda Sai
Updated on 30-Jul-2019 22:30:21

91 Views

The removeFirst() method of the java.util.LinkedList class removes and returns the first element from this list.Example:import java.util.*; public class LinkedListDemo {    public static void main(String[] args) {       LinkedList list = new LinkedList();       list.add("Hello");       list.add(2);       list.add("Chocolate");       list.add("10");       System.out.println("LinkedList:" + list);       System.out.println("First element:" + list.removeFirst());       System.out.println("LinkedList:" + list);    } }Output:LinkedList:[Hello, 2, Chocolate, 10] First element:Hello LinkedList:[2, Chocolate, 10]

What does the method getLast() do in java?

Ramu Prasad
Updated on 30-Jul-2019 22:30:21

78 Views

The getLast() method of the class java.util.LinkedList returns the last element in this list.Example:import java.util.*; public class LinkedListDemo {    public static void main(String[] args) {       LinkedList list = new LinkedList();       list.add("Hello");       list.add(2);       list.add("Chocolate");       list.add("10");       System.out.println("LinkedList:" + list);       System.out.println("Last Element :" + list.getLast());    } }Output:LinkedList:[Hello, 2, Chocolate, 10] Last Element :10

What does the method getFirst() do in java?

Ankitha Reddy
Updated on 30-Jul-2019 22:30:21

103 Views

The getFirst() method of the class java.util.LinkedList returns the first element of the current list. Example Live Demo import java.util.*; public class LinkedListDemo { public static void main(String[] args) { LinkedList list = new LinkedList(); list.add("Hello"); list.add(2); list.add("Chocolate"); list.add("10"); System.out.println("LinkedList:" + list); System.out.println("First Element :" + list.getFirst()); } } Output LinkedList:[Hello, 2, Chocolate, 10] First Element :Hello

What does the method addLast(E e) do in java?

Sravani S
Updated on 30-Jul-2019 22:30:21

126 Views

The addLast(E e) method of the java.util.LinkedList class inserts the specified element at the end of this list.Example:import java.util.*; public class LinkedListDemo {    public static void main(String[] args) {       LinkedList list = new LinkedList();       list.add("Hello");       list.add(2);       list.add("Chocolate");       list.add("10");       System.out.println("LinkedList:" + list);       list.addLast("Element");       System.out.println("LinkedList:" + list);    } }Output:LinkedList:[Hello, 2, Chocolate, 10] LinkedList:[Hello, 2, Chocolate, 10, Element]

What does the method addFirst(E e) do in java?

V Jyothi
Updated on 30-Jul-2019 22:30:21

134 Views

The addFirst(E e) method of the class java.util.LinkedList inserts the specified element at the beginning of this list.Example:public class LinkedListDemo {    public static void main(String[] args) {       LinkedList list = new LinkedList();       list.add("Hello");       list.add(2);       list.add("Chocolate");       list.add("10");       System.out.println("LinkedList:" + list);       list.addFirst("Element");       System.out.println("LinkedList:" + list);    } }Output:LinkedList:[Hello, 2, Chocolate, 10] LinkedList:[Element, Hello, 2, Chocolate, 10]

What does the method add(int i, E element) do in java?

Priya Pallavi
Updated on 20-Feb-2020 12:26:37

245 Views

The add(int index, E element) method of the java.util.ArrayList class inserts the specified element E at the specified position in this list. It shifts the element currently at that position (if any) and any subsequent elements to the right (will add one to their indices).Exampleimport java.util.ArrayList; public class ArrayListDemo {    public static void main(String[] args) {       ArrayList arrlist = new ArrayList(5);       arrlist.add(15);       arrlist.add(22);       arrlist.add(30);       arrlist.add(40);       arrlist.add(2,25);       for (Integer number : arrlist) {          System.out.println("Number = " + number);       }    } }OutputNumber = 15 Number = 22 Number = 25 Number = 30 Number = 40

What does the method add(E element) do in java?

Nikitha N
Updated on 30-Jul-2019 22:30:21

281 Views

The add(E e) method of the java.util.ArrayList class appends the specified element E to the end of the list.Example:import java.util.ArrayList; public class ArrayListDemo {    public static void main(String[] args) {       ArrayList arrlist = new ArrayList(5);       arrlist.add(15);       arrlist.add(20);       arrlist.add(25);       for (Integer number : arrlist) {          System.out.println("Number = " + number);       }    } }Output:Number = 15 Number = 20 Number = 25

What does the method removeRange(int fromIndex, int toIndex) do in java?

Srinivas Gorla
Updated on 20-Feb-2020 12:24:27

74 Views

The removeRange() method of the ArrayList class removes all of the elements from this List whose index is between fromIndex and toIndex.Exampleimport java.util.*; public class ArrayListDemo extends ArrayList{    public static void main(String[] args) {       ArrayListDemo arrlist = new ArrayListDemo();       arrlist.add(10);       arrlist.add(12);       arrlist.add(31);       System.out.println("The list:" + arrlist);       arrlist.removeRange(0,2);       System.out.println("The list after using removeRange:" + arrlist);    } }OutputThe list:[10, 12, 31] The list after using removeRange:[31]

What does the method set(int, obj o) do in java?

Abhinanda Shri
Updated on 20-Feb-2020 12:22:09

87 Views

The set() method of the ArrayList class replaces the element at the specified position in this list with the specified element.Exampleimport java.util.ArrayList; public class Sample {    public static void main(String args[]) {       ArrayList al = new ArrayList();       System.out.println("Initial size of al: " + al.size());       al.add("C");       al.add("A");       al.add("E");       al.add(1, "A2");       System.out.println("Size of al after additions: " + al.size());       System.out.println("Contents of al: " + al);       System.out.println("Size of al after deletions: " + al.size());     ... Read More

Advertisements