
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

198 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

701 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

446 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

617 Views
In this article, we will learn to generate random number strings in Java. Random numbers are vital in programming for tasks like testing, cryptography, and simulations. A common need is generating random numeric strings, such as verification codes or randomized data. This article uses two Java methods Math.random() for simplicity and the Random class for flexibility and efficiency. Problem Statement Create a program to generate a random string consisting of numeric characters. The string's length should be customizable, and each character must be selected randomly. Input num[] = { '0', '1', '2', '3', '4', '5' }; Output 33241 A random ... Read More

1K+ Views
To convert integer array list to float array, let us first create an integer array list −ArrayList < Integer > arrList = new ArrayList < Integer > (); arrList.add(25); arrList.add(50); arrList.add(100); arrList.add(200); arrList.add(300); arrList.add(400); arrList.add(500);Now, convert integer array list to float array. We have first set the size to the float array. With that, each and every value of the integer array is assigned to the float array −final float[] arr = new float[arrList.size()]; int index = 0; for (final Integer value: arrList) { arr[index++] = value; }Example Live Demoimport java.util.ArrayList; public class Demo { public static void main(String[] ... Read More

5K+ Views
In this article, we will learn how to generate a random number from an array of integers in Java by using Random class. The Random class provides methods to generate random numbers, and we will use the nextInt(int bound) method to get a random index within the bounds of our array length. Problem Statement Given an array of integers, we need to randomly select and display one element from the array using Java. Input arr = { 10, 30, 45, 60, 78, 99, 120, 140, 180, 200}; Output Random number from the array = 30 Steps to generate ... Read More

3K+ Views
To generate random BigInteger in Java, let us first set a min and max value −BigInteger maxLimit = new BigInteger("5000000000000"); BigInteger minLimit = new BigInteger("25000000000");Now, subtract the min and max −BigInteger bigInteger = maxLimit.subtract(minLimit); Declare a Random object and find the length of the maxLimit: Random randNum = new Random(); int len = maxLimit.bitLength();Now, set a new B integer with the length and the random object created above.Example Live Demoimport java.math.BigInteger; import java.util.Random; public class Demo { public static void main(String[] args) { BigInteger maxLimit = new BigInteger("5000000000000"); BigInteger minLimit = new BigInteger("25000000000"); ... Read More

254 Views
To generate custom random number 1 or -1, you need to use nextBoolean(). At first take a loop and create a Random object on each iteration −for (int i = 0; i < 5; i++) { Random rand = new Random(); }Now, use nextBoolean() to generate 1 on TRUE condition, ekse -1 −for (int i = 0; i < 5; i++) { Random rand = new Random(); if (rand.nextBoolean()) System.out.println(1); else System.out.println(-1); }Example Live Demoimport java.util.Random; public class Demo { public static void main(String[] args) { for ... Read More