
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 7442 Articles for Java

738 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

152 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

778 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

340 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

389 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

358 Views
For converting array to 1D and 2D arrays, let us first create a one-dimensional and two-dimensional array −One-DimensionalString str[] = {"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};Two-Dimensionaldoubled [][]= { {1.2, 1.3, 2.1, 4.1}, {1.5, 2.3}, {2.5, 4.4}, {3.8}, {4.9}, {3.2, 2.1, 3.2, 7.2} };Converting array to string for one-dimensional array −Arrays.toString(str);Converting array to string for two-dimensional array −Arrays.deepToString(d);Exampleimport java.util.Arrays; public class Demo { public static void main(String args[]) { String str[] = {"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}; ... Read More

3K+ Views
Sorting refers to arranging data in a particular format. Sorting algorithm specifies the way to arrange data in a particular order (ascending or descending). Problem Statement For a given array write Java program to sort integers in unsorted array. Consider the following example - Input The unsorted integer array = [10, 14, 28, 11, 7, 16, 30, 50, 25, 18] Output The unsorted integer array = [10, 14, 28, 11, 7, 16, 30, 50, 25, 18] The sorted integer array = [7, 10, 11, 14, 16, 18, 25, 28, 30, 50] Sorting Integers in Unsorted Array ... Read More

340 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);Example Live Demoimport 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; ... Read More

819 Views
In this article, we will learn to measure the time taken to sort an array in Java. We will look at two methods to calculate sorting time: using the Date class and the System.nanoTime() method. First, we will use the Date class to track the time before and after the sorting operation, allowing us to calculate the duration in milliseconds. Then, we'll utilize System.nanoTime() to capture the time with greater precision, measuring in nanoseconds and converting the result to milliseconds. Different approaches Below are the different approaches to measure the time taken to sort an array using Java− ... Read More

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