- 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
Java Program to shuffle an array using list
Let us first create a string array and convert it to a list. After that, the shuffle would be performed.
Following is the string array −
String str[] = {"A", "B", "C", "D", "E"};
Convert the string array to list −
List<String>list = Arrays.asList(str);
Now shuffle the array −
Collections.shuffle(list);
Example
import java.util.Arrays; import java.util.Collections; import java.util.List; public class Demo { public static void main(String[] args) { String str[] = {"A", "B", "C", "D", "E"}; List<String>list = Arrays.asList(str); Collections.shuffle(list); System.out.println("Shuffled the array using List = "+list.toString()); } }
Output
Shuffled the array using List = [C, B, E, D, A]
Advertisements