
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

612 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

250 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

5K+ Views
To generated random integer, use the Random class with nextInt. At first, create a Random object −Random rand = new Random();The Random above is a random number generator. Now, pick the random numbers one by one. We want 10 random four-digit numbers, therefore loop it until i = 1 to 10 −for (int i = 1; i

193 Views
Let us first create an ArrayList −ArrayListarr = new ArrayList(); arr.add("50"); arr.add("100"); arr.add("150"); arr.add("200"); arr.add("250"); arr.add("300");Now, let’s say we have another Collection as Vector −Vectorvector = new Vector(); vector.add("500"); vector.add("700"); vector.add("800"); vector.add("1000"); Append all the elements from the Vector to ArrayList: arr.addAll(vector);Example Live Demoimport java.util.ArrayList; import java.util.Vector; public class Demo { public static void main(String[] args) { ArrayListarr = new ArrayList(); arr.add("50"); arr.add("100"); arr.add("150"); arr.add("200"); arr.add("250"); arr.add("300"); Vectorvector = new Vector(); vector.add("500"); ... Read More

300 Views
Let us first create an ArrayList −ArrayList < Integer > arrList = new ArrayList < Integer > (); arrList.add(100); arrList.add(200); arrList.add(300); arrList.add(400); arrList.add(500);Now, create a ListIterator from the above ArrayList and insert more elements −ListIterator < Integer > iterator = arrList.listIterator(); iterator.add(1000); iterator.add(2000); iterator.add(3000);Example Live Demoimport java.util.ArrayList; import java.util.ListIterator; public class Demo { public static void main(String[] args) { ArrayListarrList = new ArrayList(); arrList.add(100); arrList.add(200); arrList.add(300); arrList.add(400); arrList.add(500); arrList.add(600); arrList.add(700); arrList.add(800); ... Read More

694 Views
Let us first create an ArrayList −ArrayListarrList = new ArrayList(); arrList.add(10); arrList.add(50); arrList.add(100); arrList.add(150); arrList.add(250);Use Comparators interface to order in reverse order with reverseOrder() −Comparator comparator = Collections.reverseOrder(); Now, sort with Collections: Collections.sort(arrList, comparator);Example Live Demoimport java.util.ArrayList; import java.util.Collections; import java.util.Comparator; public class Demo { public static void main(String[] args) { ArrayListarrList = new ArrayList(); arrList.add(10); arrList.add(50); arrList.add(100); arrList.add(150); arrList.add(250); arrList.add(100); arrList.add(150); arrList.add(250); Comparator comparator = Collections.reverseOrder(); ... Read More

3K+ Views
Suppose a date is given in LocalDate format, our task is to write a Java program that converts it into java.util.Date. For this problem, we need to add time information to the LocalDate by combining it with a time, such as midnight, or a timezone. Both LocalDate and java.util.Date classes are used to represent a date in Java. But, the LocalDate class, introduced with the release of Java8, represents a date without timezone information, whereas, the Date class is mutable and used for representing date and time with timezone. Let's understand the problem statement with an example. Example Scenario: Input: ... Read More