Convert Array to Generic List with Java Reflections

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

703 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

Compare Two Long Arrays in a Single Line in Java

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

207 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

Binomial Coefficient in Java

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

5K+ Views

Binomial coefficient (c(n, r) or nCr) is calculated using the formula n!/r!*(n-r)!. Following is the Java program find out the binomial coefficient of given integers.Programimport java.util.Scanner; public class BinomialCoefficient {    public static long fact(int i) {       if(i

Sort Subset of Array Elements in Java

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

5K+ 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

Number.isFinite Function in JavaScript

Samual Sam
Updated on 25-Jun-2020 12:40:36

84 Views

The isFinite() function of the Number object accepts a number and determines if it is finite number. If the given number is finite it returns true else, it returns false.SyntaxIts Syntax is as followsNumber.isFinite(100/0);Example Live Demo    JavaScript Example           var result1 = Math.min();       document.write(isFinite(result1));       document.write("");       var result2 = Number.isFinite(100/0);       document.write(result2);       document.write("");       var result3 = Math.max(25, 36, 862);       document.write(isFinite(result3));       document.write("");       document.write(isFinite("Hello"));     Outputfalse false true false

Number.isInteger Function in JavaScript

karthikeya Boyini
Updated on 25-Jun-2020 12:40:01

87 Views

The isInteger() function of the Number object accepts a number and determines if it is finite number. If the given number is finite it returns true else, it returns false.SyntaxIts Syntax is as followsNumber.isInteger(100/0);Example Live Demo    JavaScript Example           var result = Number.isFinite(100/0);       if(result) {          document.write("Given number is finite");       }else {          document.write("Given number is infinite");       }     OutputGiven number is infiniteExampleIn addition to equation you can also pass positive number, negative number, zero and ... Read More

Multiplicative Order in Java

Arjun Thakur
Updated on 25-Jun-2020 12:39:40

155 Views

Following is a Java program which prints the multiplicative order of given numbers.import java.util.Scanner;Programpublic class MultiplicativeOrder {    public static int gcd(int num1, int num2) {       if (num2 != 0) {          return gcd(num2, num1 % num2);       } else {          return num1;       }    }    static int multiplicativeOrder(int num1, int num2) {       if (gcd(num1, num2) != 1) {          return -1;       }       int res = 1;       int p ... Read More

Sort Arrays of Objects in Java

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

528 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

Math.round() Function in JavaScript

karthikeya Boyini
Updated on 25-Jun-2020 12:38:53

161 Views

The round() function of the Math object accepts a floating point random number and returns its nearest integer value.If the given number is x.5 or more this function returns the next number (x+1)If the given number is x.4 or less this function returns the previous number (x-1).If the given number itself is an integer this function returns the same.SyntaxIts Syntax is as followsMath.round();Example Live Demo    JavaScript Example           var result = Math.round(2541.542);       document.write("Rounded value of the given number: "+result);     OutputRounded value of the given number: 2542

Math.sign() Function in JavaScript

Samual Sam
Updated on 25-Jun-2020 12:38:31

117 Views

The sign() function of the Math object accepts an integer number and returns its sign.(positive, negative)If the given number is a positive integer then, this function returns +1.And, if the given number is a negative integer then, this function returns -1.SyntaxIts Syntax is as followsMath.sign();Example Live Demo    JavaScript Example           var result = Math.sign(-128);       document.write("Result: "+result);     OutputResult: -1

Advertisements