Found 9326 Articles for Object Oriented Programming

Compare two float arrays in a single line in Java

Nancy Den
Updated on 25-Jun-2020 12:29:39

144 Views

Two float arrays can be compared in Java using the java.util.Arrays.equals() method. This method returns true if the arrays are equal and false otherwise. The two arrays are equal if they contain the same number of elements in the same order.A program that compares two float arrays using the Arrays.equals() method is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] argv) throws Exception {       boolean flag = Arrays.equals(new float[] { 1.5F, 8.3F, 7.6F }, new float[] { 1.5F, 8.3F, 7.6F });       System.out.println("The two float arrays are equal? ... Read More

Get class from an object in Java

Krantik Chavan
Updated on 25-Jun-2020 12:30:16

201 Views

The runtime class of an object can be obtained using the java.lang.Object.getClass(). Also, the getName() method is used to get the name of the class.A program that demonstrates getting the class of an object using the getClass() method is given as follows −Example Live Demopublic class Demo {    public static void main (String [] args) {       Integer integer = new Integer(10);       Class c = integer.getClass();       System.out.println ("The class is: " + c.getName());       c = new Demo().getClass();       System.out.println ("The class is: " + c.getName());    } ... Read More

Java Program to implement Binary Search on an array

George John
Updated on 25-Jun-2020 12:34:59

303 Views

Binary search can be implemented on an array by using the method java.util.Arrays.binarySearch(). This method returns the index of the required element if it is available in the array, otherwise it returns (- (insertion point) - 1) where the insertion point is the position at which the element would be inserted into the array.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       int arr[] = { 3, 9, 1, 6, 4};       Arrays.sort(arr);       System.out.print("The sorted array is: "); ... Read More

How to extend the size of an array in Java

Ankith Reddy
Updated on 25-Jun-2020 12:36:05

407 Views

An array has a fixed number of values and its size is defined when it is created. Therefore, the size of the array cannot be changed later. To solve this problem, an ArrayList should be used as it implements a resizable array using the List interface.A program that demonstrates ArrayList in Java is given as follows −Example Live Demoimport java.util.*; public class Demo {    public static void main(String args[]) {       ArrayList aList = new ArrayList();       aList.add("Apple");       aList.add("Melon");       aList.add("Orange");       aList.add("Mango");       aList.add("Grapes");     ... Read More

Search elements in a sorted object array in Java

Arjun Thakur
Updated on 25-Jun-2020 12:38:00

152 Views

An element can be searched in a sorted object array in Java by using the methodjava.util.Arrays.binarySearch(). This method returns the index of the required element if it is available in the array, otherwise it returns (-(insertion point) - 1) where the insertion point is the position at which the element would be inserted into the array. A program that searches the required element in a sorted object array is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       String str[] = { "P", "M", "A", "T", "D"};     ... Read More

Sort arrays of objects in Java

Chandu yadav
Updated on 25-Jun-2020 12:39:27

402 Views

An array of objects can be sorted using the java.util.Arrays.sort() method with a single argument required i.e. the array to be sorted. A program that demonstrates this is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String args[]) throws Exception {       String str[] = new String[]{"apple", "orange", "mango", "guava", "melon"};       int n = str.length;       System.out.println("The original array of strings is: ");       for (int i = 0; i < n; i++) {          System.out.println(str[i]);       }     ... Read More

How to schedule tasks in Java to run for repeated fixed-rate execution, beginning at the specified time

Anvi Jain
Updated on 25-Jun-2020 12:33:59

2K+ Views

One of the methods the Timer class is void scheduleAtFixedRate(TimerTask task, Date firstTime, long period). This method schedules the specified task for repeated fixed-rate execution, beginning at the specified time.In fixed-rate execution, each execution is scheduled with respect to the scheduled run time of the initial execution. Fixed-rate execution is apt for repetitive activities that are respond to absolute time. Likewise, fixed-rate execution is appropriate for scheduling multiple repeating timer tasks that must remain in sync.Declaration − The java.util.Time.scheduleAtFixedRate(TimerTask task, Date firstTime, long period) method is declared as follows −public void scheduleAtFixedRate(TimerTask task, Date firstTime, long period)Here, task is the ... Read More

Sort subset of array elements in Java

George John
Updated on 25-Jun-2020 12:41:05

4K+ Views

The java.util.Arrays.sort() method can be used to sort a subset of the array elements in Java. This method has three arguments i.e. the array to be sorted, the index of the first element of the subset (included in the sorted elements) and the index of the last element of the subset (excluded from the sorted elements). Also, the Arrays.sort() method does not return any value.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       int arr[] = { 1, 9, 7, 3, 2, 8, ... Read More

Compare two long arrays in a single line in Java

Ankith Reddy
Updated on 25-Jun-2020 12:41:40

118 Views

Two long arrays can be compared in Java using the java.util.Arrays.equals() method. This method returns true if the arrays are equal and false otherwise. The two arrays are equal if they contain the same number of elements in the same order.A program that compares two long arrays using the Arrays.equals() method is given as follows −Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] argv) throws Exception {       boolean flag = Arrays.equals(new long[] { 450L, 150L, 375L }, new long[] { 450L, 150L, 375L });       System.out.println("The two long arrays are equal? ... Read More

Convert array to generic list with Java Reflections

Arjun Thakur
Updated on 25-Jun-2020 12:42:10

529 Views

An array can be converted into a fixed size list using the java.util.Arrays.asList() method. This method is basically a bridge between array based AP!’s and collection based API’s.A program that demonstrates the conversion of an array into a generic list is given as follows −Example Live Demoimport java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo {    public static void main(String[] args) {       String str[] = new String[]{"apple", "orange", "mango", "guava", "melon"};       List list = Arrays.asList(str);       System.out.println("The list is: " + list);    } }The output of the above program is as ... Read More

Advertisements