- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 4336 Articles for Java 8
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
937 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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
327 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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
391 Views
At first, create a character array −static char num[] = { '0', '1', '2', '3', '4', '5' };Now, let’s say you want to a string with length. Create a StringBuilder and use append() to create random numbers string out of it −int len = 5; StringBuilder strBuilder = new StringBuilder(); for (int i = 0; i < len; i++) { strBuilder.append(randomNum()); }Above, we created a randomNum() function that returns the random numbers string −public static char randomNum() { return num[(int) Math.floor(Math.random() * 5)]; }Example Live Demopublic class Demo { static char num[] = { '0', '1', '2', '3', ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
966 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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
3K+ 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 a ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
2K+ 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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
165 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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
4K+ 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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
136 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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
233 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