
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 33676 Articles for Programming

355 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

339 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

811 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

196 Views
To output fixed number of array elements in a line, we should check for a condition and if that is true, we should place System.out.println(); properly to get a line and the process goes on.Here, we will display 5 elements in a line. At first, create a new integer array and add some elements −int[] list = new int[50]; for (int i = 0; i < list.length; i++) { list[i] = (int)(i + 20); }Now, declare a new variable and initialize it to 0. This variable is checked in a for loop that loops until the length of the ... Read More

695 Views
Here, to convert Wrapper value array list into primitive array, we are considering Integer as Wrapper, whereas double as primitive.At first, declare an Integer array list and add elements to it −ArrayList < Integer > arrList = new ArrayList < Integer > (); arrList.add(5); arrList.add(10); arrList.add(15); arrList.add(20); arrList.add(25); arrList.add(30); arrList.add(45); arrList.add(50);Now, convert the above Integer array list to primitive array. Firstly, we set the same size for the double array and then assigned each valuefinal double[] arr = new double[arrList.size()]; int index = 0; for (final Integer value: arrList) { arr[index++] = value; }The following is an example to ... Read More

1K+ Views
Let us first declare a string array and initialize −String[] strArr = { "P", "Q", "R", "S", "T", "U", "V", "W" };Now, create a Random object −Random rand = new Random();Generate random string −int res = rand.nextInt(strArr.length);Example Live Demoimport java.util.Random; public class Demo { public static void main(String[] args) { String[] strArr = { "P", "Q", "R", "S", "T", "U", "V", "W" }; Random rand = new Random(); int res = rand.nextInt(strArr.length); System.out.println("Displaying a random string = " + strArr[res]); } }OutputDisplaying a random string = RLet ... Read More

443 Views
To generate random values that won’t repeat, use HashSet collection. Firstly, create a random object and HashSet −Random randNum = new Random(); Sets = new HashSet();Now, add random integers −while (s.size() < 10) { s.add(randNum.nextInt()); }Now, display the random numbers that are unique −Listlist = new ArrayList(s); System.out.println(list);Example Live Demoimport java.util.ArrayList; import java.util.HashSet; import java.util.Set; import java.util.Random; import java.util.List; public class Demo { public static void main(String[] args) { Random randNum = new Random(); Sets = new HashSet(); while (s.size() < 10) { s.add(randNum.nextInt()); ... Read More