Found 9150 Articles for Object Oriented Programming

What does the method clone() do in java?

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

185 Views

The clone() method of the java.util.ArrayList class returns a shallow copy of this ArrayList instance(i.e. the elements themselves are not copied). Example import java.util.ArrayList; public class ArrayListDemo { public static void main(String args[]) { ArrayList arrlist1 = new ArrayList(); arrlist1.add(new StringBuilder("Learning-")); ArrayList arrlist2 = (ArrayList) arrlist1.clone(); StringBuilder strbuilder = arrlist1.get(0); strbuilder.append("list1, list2-both pointing to the same StringBuilder"); ... Read More

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(int) do in java?

Govinda Sai
Updated on 25-Feb-2020 10:19:36

116 Views

The remove(int index) method of the java.util.ArrayList class removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).Exampleimport java.util.ArrayList; public class ArrayListDemo {    public static void main(String[] args) {       ArrayList arrlist = new ArrayList(5);       arrlist.add(20);       arrlist.add(15);       arrlist.add(30);       arrlist.add(45);       System.out.println("Size of list: " + arrlist.size());       for (Integer number : arrlist) {          System.out.println("Number = " + number);       }       ... Read More

What does the method copyOfRange(int[] original, int from, int to) do in java?

Samual Sam
Updated on 13-Jun-2020 12:29:44

204 Views

The copyOfRange(int[] original, int from, int to) method of java.util.Arrays class copies the specified range of the specified array into a new array. The final index of the range (to), which must be greater than or equal to from, may be greater than original. Length, in which case 0 is placed in all elements of the copy whose index is greater than or equal to original. Length - from. The length of the returned array will be to - from.Exampleimport java.util.Arrays; public class ArrayDemo {    public static void main(String[] args) {       int[] arr1 = new ... Read More

What does the method copyOf(int[] original, int newLength) do in java?

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

252 Views

The copyOf (int[] original, int newLength) method of the java.util.Arrays class copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length. For all indices that are valid in both the original array and the copy, the two arrays will contain identical values. For any indices that are valid within the copy however not the original, the copy can contain zero. Such indices will exist if and only if the specified length is greater than that of the original array. Example import java.util.Arrays; public class ArrayDemo { public ... Read More

What does the method equals(obj[] a1, obj[] a2) do in java?

Lakshmi Srinivas
Updated on 20-Feb-2020 12:46:45

212 Views

The equals(Object[] a, Object[] a2) method of the java.util.Arrays class returns true if the two specified arrays of objects are equal to one another. The two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. Two objects e1 and e2 are considered equal if (e1==null ? e2==null : e1.equals(e2)). The two arrays are equal if they contain the same elements in the same order. Two array references are considered equal if both are null.Exampleimport java.util.Arrays; public class ArrayDemo {    public static void main(String[] ... Read More

What does the method equals(int[] a1, int[] a2) do in java?

Monica Mona
Updated on 20-Feb-2020 12:46:02

303 Views

The equals(int[] a, int[] a2) method of java.util.Arrays returns true if the two specified arrays of integers are equal to one another. Two arrays are equal if they contain the same elements in the same order. Two array references are considered equal if both are null.Exampleimport java.util.Arrays; public class ArrayDemo {    public static void main(String[] args) {       int[] arr1 = new int[] { 10, 12, 5, 6 };       int[] arr2 = new int[] { 10, 12, 5, 6 };       int[] arr3 = new int[] { 10, 5, 6, 12 };     ... Read More

What does the method fill(obj[], int fromIndex, int toIndex, int val) do in java?

karthikeya Boyini
Updated on 25-Feb-2020 08:21:52

156 Views

The fill(object[] a, int fromIndex, int toIndex, object val) method of the class java.util.Arrays assign the specified Object reference to each element of the specified range of the specified array of objects. The range to be filled extends from index fromIndex, inclusive, to index toIndex, exclusive. (If fromIndex==toIndex, the range to be filled is empty)Exampleimport java.util.Arrays; public class ArrayDemo {    public static void main(String[] args) {       Object arr[] = new Object[] {1.2, 5.6, 3.4, 2.9, 9.7};       System.out.println("Actual values: ");       for (Object value : arr) {          System.out.println("Value ... Read More

What does the method fill(int[], int fromIndex, int toIndex, int val) do in java?

Swarali Sree
Updated on 25-Feb-2020 09:27:53

200 Views

The fill(int[] a, int fromIndex, int toIndex, int val) method of the java.util.Arrays class assigns the specified int value to each element of the specified range of the specified array of integers. The range to be filled extends from index fromIndex, inclusive, to index toIndex, exclusive.(If fromIndex==toIndex, the range to be filled is empty.)Exampleimport java.util.Arrays; public class ArrayDemo {    public static void main(String[] args) {       int arr[] = new int[] {1, 6, 3, 2, 9};       System.out.println("Actual values: ");       for (int value : arr) {          System.out.println("Value = ... Read More

What does the method fill(int[], int val) do in java?

Samual Sam
Updated on 20-Feb-2020 12:42:04

134 Views

The fill(int[] a, int val) method of the java.util.Arrays class assigns the specified int value to each element of the specified array of integers.Exampleimport java.util.Arrays; public class ArrayDemo {    public static void main(String[] args) {       int arr[] = new int[] {1, 6, 3, 2, 9};       System.out.println("Actual values: ");       for (int value : arr) {          System.out.println("Value = " + value);       }       Arrays.fill(arr, 18);       System.out.println("New values after using fill() method: ");       for (int value : arr) {          System.out.println("Value = " + value);       }    } }OutputActual values: Value = 1 Value = 6 Value = 3 Value = 2 Value = 9 New values after using fill() method: Value = 18 Value = 18 Value = 18 Value = 18 Value = 18

Advertisements