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
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 (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
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
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
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
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
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
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
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
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP