lastIndexOf Method in Java

Ramu Prasad
Updated on 20-Feb-2020 12:20:20

122 Views

The lastIndexOf(Object) method of the class java.util.ArrayList returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.Exampleimport java.util.ArrayList; public class ArrayListDemo {    public static void main(String[] args) {       ArrayList arrlist = new ArrayList(5);       arrlist.add("G");       arrlist.add("E");       arrlist.add("F");       arrlist.add("M");       arrlist.add("E");       System.out.println("Size of list: " + arrlist.size());       for (String value : arrlist) {          System.out.println("Value = " + value);     ... Read More

What Does the Method indexOf(obj o) Do in Java

Sravani S
Updated on 20-Feb-2020 12:19:30

176 Views

The indexOf(Object) method of the java.util.ArrayList class returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.Exampleimport java.util.ArrayList; public class ArrayListDemo {    public static void main(String[] args) {       ArrayList arrlist = new ArrayList(5);       arrlist.add("G");       arrlist.add("E");       arrlist.add("F");       arrlist.add("M");       System.out.println("Size of list: " + arrlist.size());       for (String value : arrlist) {          System.out.println("Value = " + value);       }     ... Read More

What Does the Method getInt Do in Java

V Jyothi
Updated on 20-Feb-2020 12:18:14

528 Views

The get(int index) method of the java.util.ArrayList class returns the element at the specified position in this list.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);       for (Integer number : arrlist) {          System.out.println("Number = " + number);       }       int retval=arrlist.get(3);       System.out.println("Retrieved element is = " + retval);    } }OutputNumber = 15 Number = 22 Number = 30 Number = 40 Retrieved element is = 40

What Does the Method isEmpty Do in Java?

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

201 Views

The isEmpty() method of the class java.util.ArrayList returns true if this list contains no elements.Exampleimport java.util.ArrayList; public class ArrayListDemo {    public static void main(String[] args) {       ArrayList arrlist = new ArrayList(5);       arrlist.add(25);       arrlist.add(10);       arrlist.add(20);       arrlist.add(35);       boolean retval = arrlist.isEmpty();       if (retval == true) {          System.out.println("list is empty");       } else {          System.out.println("list is not empty");       }       for (Integer number : arrlist) {          System.out.println("Number = " + number);       }    } }Outputlist is not empty Number = 25 Number = 10 Number = 20 Number = 35

What Does the Method Size Do in Java

Abhinanda Shri
Updated on 20-Feb-2020 12:16:41

845 Views

The size() method of the class java.util.ArrayList returns the number of elements in this list i.e. the size of the list.Exampleimport 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);       arrlist.add(22);       for (Integer number : arrlist) {          System.out.println("Number = " + number);       }       int retval = arrlist.size();       System.out.println("Size of list = " + retval);    } }OutputNumber = 15 Number = 20 Number = 25 Number = 22 Size of list = 4

What Does the Method clear() Do in Java

Abhinaya
Updated on 20-Feb-2020 12:15:41

1K+ Views

The clear() method of the class java.util.ArrayList removes all of the elements from this list. The list will be empty after this call returns.Exampleimport java.util.ArrayList; public class ArrayListDemo {    public static void main(String[] args) {       ArrayList arrlist = new ArrayList(5);       arrlist.add(20);       arrlist.add(30);       arrlist.add(10);       arrlist.add(50);       for (Integer number : arrlist) {          System.out.println("Number = " + number);       }       int retval = arrlist.size();       System.out.println("List consists of "+ retval +" elements"); ... Read More

What Does the Method remove(obj) Do in Java?

Sravani S
Updated on 20-Feb-2020 12:14:54

151 Views

The remove(Object) method of the class java.util.ArrayList removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged.Exampleimport java.util.ArrayList; public class ArrayListDemo {    public static void main(String[] args) {       ArrayList arrlist = new ArrayList(5);       arrlist.add("G");       arrlist.add("E");       arrlist.add("F");       arrlist.add("M");       arrlist.add("E");       System.out.println("Size of list: " + arrlist.size());       for (String value : arrlist) {          System.out.println("Value = " + ... Read More

Custom UnaryOperator Implementation in Java

Maruthi Krishna
Updated on 20-Feb-2020 12:04:09

449 Views

The java.util.function.UnaryOperator interface and can be used as assignment target for lambda expressions, it represents operation on a single operand whose result will be of same type as the input. We can create our own UnaryOperator by implementing this interface.The replaceAll() method of the List interface accept an object of the UnaryOperator representing a particular operation performs the specified operation on all the elements of the current list and replaces the existing values with the resultant values.In the following example we are implementing the UnaryOperator interface and creating a custom unary operator object and trying to pass it as an ... Read More

What Does the Method addAll(int, Coll) Do in Java

Ramu Prasad
Updated on 20-Feb-2020 11:30:57

108 Views

The addAll(int index, Collection

Advertisements