Java Articles

Page 63 of 450

How to populate a 2d array with random alphabetic values from a range in Java?

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 1K+ Views

To populate a 2d array with random alphabets, use the Random class. Let us first declare a 2d array −char arr[][] = new char[3][3];Now, in a nested for loop, use the Random class object to get random values on the basis of switch case. Here, our range is 3 i.e. set of 3 alphabets at once −Random randNum = new Random(); for (int i = 0; i < 3; i++) {    for (int j = 0; j < 3; j++) {       int x = randNum.nextInt(3);       switch (x) {          case ...

Read More

Java Program to sort a subset of array elements

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 385 Views

Let us first create a string array −String[] strArr = new String[] { "r", "p", "v", "y", "s", "q" };Now, use Arrays.sort() to get the subset. Use the following to sort only from the index range 2 to 6.Arrays.sort(strArr, 2, 6);Exampleimport java.util.Arrays; public class Demo {    public static void main(String[] args) {       String[] strArr = new String[] { "r", "p", "v", "y", "s", "q" };       Arrays.sort(strArr, 2, 6);       System.out.println("Sorted subset of array elements from index 2 to 6...");       for (int a = 0; a < strArr.length; a++) ...

Read More

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

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 189 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); }Exampleimport 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

How to randomize and shuffle array of numbers in Java?

Samual Sam
Samual Sam
Updated on 11-Mar-2026 790 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; }Exampleimport java.util.Arrays; import java.util.Random; public class Demo {    public static void main(String[] args) {       int[] arr = { ...

Read More

How to shuffle an array in Java?

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 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); }Exampleimport java.util.Arrays; import java.util.List; import java.util.Random; public class Demo {    public static void main(String[] args) {     ...

Read More

Java Program to shift array elements to the right

Samual Sam
Samual Sam
Updated on 11-Mar-2026 558 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);Exampleimport 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]

Read More

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

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 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 generate random elements from a given array

Samual Sam
Samual Sam
Updated on 11-Mar-2026 554 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 + " "); }Exampleimport 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 get minimum value with Comparator

karthikeya Boyini
karthikeya Boyini
Updated on 11-Mar-2026 428 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));Exampleimport 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 convert java.util.Date to LocalDate

Samual Sam
Samual Sam
Updated on 11-Mar-2026 354 Views

At first set the date with java.util.Date −java.util.Date date = new Date();Now, convert the date to LocalDate −Instant instant = Instant.ofEpochMilli(date.getTime()); System.out.println("LocalDate = "+LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate());Exampleimport java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.util.Date; public class Demo {    public static void main(String[] args) {       java.util.Date date = new Date();       System.out.println("Date = "+date);       Instant instant = Instant.ofEpochMilli(date.getTime());       System.out.println("LocalDate = "+LocalDateTime.ofInstant(instant, ZoneId.systemDefault()).toLocalDate());    } }OutputDate = Thu Apr 18 23:51:06 IST 2019 LocalDate = 2019-04-18

Read More
Showing 621–630 of 4,498 articles
« Prev 1 61 62 63 64 65 450 Next »
Advertisements