Found 33676 Articles for Programming

Java Program to get minimum value with Comparator

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

376 Views

First, declare an integer array and add some elements −Integer arr[] = { 40, 20, 30, 10, 90, 60, 700 };Now, convert the above array to List −List list = Arrays.asList(arr);Now, use Comparator and the reverseOrder() method. Using max() will eventually give you the maximum value, but with reverseOrder() it reverses the result −Comparator comp = Collections.reverseOrder(); System.out.println("Minimum element = "+Collections.max(list, comp));Example Live Demoimport java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Demo {    @SuppressWarnings("unchecked")    public static void main(String args[]) {       Integer arr[] = { 40, 20, 30, 10, 90, 60, 700 };     ... Read More

Java Program to generate random elements from a given array

Samual Sam
Updated on 30-Jul-2019 22:30:25

503 Views

Let’s say the following is our array −Integer[] arr = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};Now, we can convert it to a list before shuffling it −Listlist = Arrays.asList(arr); Collections.shuffle(list);The above shuffling generates random elements. Display them like this −for (Integer res: list) {    System.out.print(res + " "); }Example Live Demoimport java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo {    public static void main(String[] args) {       Integer[] arr = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};       System.out.print("Array elements...");       for (Integer res: arr) { ... Read More

Java Program to generate random number array within a range and get min and max value

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

1K+ Views

At first, create a double array −double[] val = new double[10];Now, generate and display random numbers in a loop that loops until the length of the above array. We have used nextInt here for random numbers −for (int i = 0; i < val.length; i++) {    val[i] = new Random().nextInt(100);    System.out.println(val[i]); }Now, get the min and max values. Compare each value of the random array with the MIN and MAX values −double min = Double.MAX_VALUE; double max = Double.MIN_VALUE; for (int i = 0; i < val.length; i++) {    if (val[i] < min)       min ... Read More

Java Program to shift array elements to the right

Samual Sam
Updated on 30-Jul-2019 22:30:25

469 Views

Let us first create an int array −int[] arr = { 10, 20, 30, 40, 50, 60, 70, 80, 90 };Now, shift array elements to the right with arraycopy() and placing the elements correctly so that it gets shifted to the right −System.arraycopy(arr, 0, arr, 1, arr.length - 1);Example Live Demoimport java.util.Arrays; public class Demo {    public static void main(String[] argv) throws Exception {       int[] arr = { 10, 20, 30, 40, 50, 60, 70, 80, 90 };       System.out.println("Initial array..."+Arrays.toString(arr));       System.arraycopy(arr, 0, arr, 1, arr.length - 1);       System.out.println("Array after shifting to the right...");       System.out.println(Arrays.toString(arr));    } }OutputInitial array... [10, 20, 30, 40, 50, 60, 70, 80, 90] Array after shifting to the right... [10, 10, 20, 30, 40, 50, 60, 70, 80]

How to shuffle an array in Java?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

3K+ Views

Declare a string array and add elements in the form of letters −String[] letters = { "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };Convert the above array to list −Listlist = Arrays.asList(letters);Now, create a shuffled array using the Random class object and generate the random letters with nextInt() −int len = list.size(); System.out.println("Shuffled array..."); for (int i = 0; i < letters.length; i++) {    int index = new Random().nextInt(len);    String shuffle = list.get(index);    System.out.println(shuffle); }Example Live Demoimport java.util.Arrays; import java.util.List; import java.util.Random; public class Demo {    public static void main(String[] args) {   ... Read More

How to randomize and shuffle array of numbers in Java?

Samual Sam
Updated on 30-Jul-2019 22:30:25

733 Views

At first, create an integer array −int[] arr = { 20, 40, 60, 80, 100, 120, 140, 160, 180, 200};Now, create a Random class object −Random rand = new Random();Loop until the length of the array and shuffle the elements −for (int i = 0; i < arr.length; ++i) {    int index = rand.nextInt(arr.length - i);    int tmp = arr[arr.length - 1 - i];    arr[arr.length - 1 - i] = arr[index];    arr[index] = tmp; }Example Live Demoimport java.util.Arrays; import java.util.Random; public class Demo {    public static void main(String[] args) {       int[] arr = ... Read More

Java Program to create an array with randomly shuffled numbers in a given range

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

150 Views

For randomly shuffled numbers, let us create an integer list and add some numbers −List < Integer > list = new ArrayList < Integer > (); list.add(10); list.add(50); list.add(100); list.add(150); list.add(200);Now, shuffle the elements for random values −Collections.shuffle(list);Create an int array with the same number of elements in the above list −intlen = list.size(); int[] res = newint[len];Set the shuffled values in the new array −for (inti = 0; i < len; i++) {    res[i] = list.get(i); }Example Live Demoimport java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo {    public static void main(String args[]) {     ... Read More

Java Program to get maximum value with Comparator

Alshifa Hasnain
Updated on 26-Dec-2024 20:42:05

771 Views

In Java, the Comparator interface is used to define a custom ordering for objects in various Collections. This can be particularly useful when you need to find the maximum or minimum element based on specific criteria rather than natural ordering. One common use case is finding the maximum value in a collection using a comparator. Java provides the Collections utility class, which includes the max() and min() methods that accept a comparator to perform customized comparisons. In this article, we will explore how to use these methods to find the maximum value from a list using a comparator. Finding Maximum ... Read More

Java Program to extend the size of an Integer array

karthikeya Boyini
Updated on 10-Feb-2025 11:32:23

334 Views

In this article, we will learn how to extend the size of an Integer array in Java. We will see how to create a new array with an extended size and copy the existing array elements into it. What is an array? An array is a data structure that holds a collection of elements (values or variables) of the same size in memory, with each element being identified by one or more indices or keys.How does Java arraycopy() transfer array elements?The Java arraycopy() method copies elements from a source array to a destination array. It begins at a specified position ... Read More

How can we copy one array from another in Java

Samual Sam
Updated on 30-Jul-2019 22:30:25

387 Views

Let us first create a string array −String[] arr = new String[] { "P", "Q", "R", "S", "T"};Now, calculate the length of the above array and create a new array with the same length −int len = arr.length; String[] arr2 = new String[len];Let us copy one array from another −System.arraycopy(arr, 0, arr2, 0, arr.length);Example Live Demopublic class Demo {    public static void main(String[] args) {       String[] arr = new String[] { "P", "Q", "R", "S", "T"};       System.out.println("Initial array...");       for (int i = 0; i < arr.length; i++)       System.out.println(arr[i]); ... Read More

Advertisements