Home >Java >javaTutorial >Java program to shuffle vector elements
Shuffle() is a collection class method in Java that works in a random manner based on the permutation logic of a specific group of list elements. Tree are two different types of methods in shuffle class(), depending on specific parameters.
Java collection shuffle(list) method.
Java Collections shuffle(list, random) method. In this method, we can randomly arrange the characters to generate some random values. Then we will apply the suffle method to it.
To perform vector shuffling, we can use the Fisher-Yates shuffling algorithm. In this approach we can linearly scan the vector and swap each element with a random element.
In today's article, we will learn how to shuffle vector elements using the Java environment.
This is a possible algorithm in Java code, how we can shuffle the elements of a vector containing strings.
Step 1 - Get Started.
Step 2 - Declare the shuffle package that exists in the Java environment.
Step 3 - Declare a function for shuffling cards.
Step 4 - If the operation is to shuffle a random vector, declare it.
Step 5 - Declare a public class.
Step 6 - Get an input array vector.
Step 7 - Mention the length of the array.
Step 8 - If the declaration is random, declare it.
Step 9 - Continue to the next step.
Step 10 - Use a for loop to run the method.
Step 11 - Iterate over the value.
Step 12 - Decrease the value if necessary.
Step 13 - Swap and change positions.
Step 14 - Attend Assistance Course
Step 15 - Declare the change value as equal to the helper class.
Step 16 - Enter the parameter string.
Step 17 - Enter the int string.
Step 18 - Declare the subarray.
Step 19 - Request a printout.
Step 20 - Termination.
General Syntax: public static void shuffle(List<?> list) public static void shuffle(List<?> list, Random random) Possible Code Syntax: public class Main { public static void main(String[] args) { Vector<String> v = new Vector<String>(); v.add("16"); v.add("07"); v.add("10"); v.add("2001"); v.add("1997"); System.out.println(v); Collections.shuffle(v); System.out.println(v); } } Double Shuffle: import java.util.*; public class CollectionsShuffleTutorialspoint { public static void main(String[] args) { List<String> list = Arrays.asList("X", "Y", "R", "A"); System.out.println("List Before Shuffle Here ----> : "+list); Collections.shuffle(list); System.out.println("List After Shuffle Is Here ----> : "+list); } }
Here we have mentioned the possible syntax related to the shuffle method. You can see that the double shuffling process also works for vector elements. With these possible syntaxes, we try to later build some Java code to shuffle vector elements in a specific string.
Method 1 - Java program to shuffle vector elements
Method 2−Fisher-Yates Shuffling Algorithm
Here we include some Java build code that allows us to shuffle some vector elements in a simple and random way.
The Chinese translation ofimport java.util.Collections; import java.util.Vector; public class VectorShuffleElements { public static void main(String[] args) { Vector<Integer> vNumbers = new Vector<Integer>(); vNumbers.add(16); vNumbers.add(07); vNumbers.add(2001); vNumbers.add(1997); vNumbers.add(10); Collections.shuffle(vNumbers); System.out.println("Vector contains are present in the list:----> " + vNumbers); } }
Vector contains are present in the list:----> [16, 2001, 7, 10, 1997]
import java.util.Vector; import java.util.Collections; public class Tutorialspoint { public static void main(String[] args){ Vector<String> vec07 = new Vector<String>(); vec07.add("10"); vec07.add("16"); vec07.add("7"); vec07.add("2001"); vec07.add("1997"); System.out.println("Original Vector is here ----> : " + vec07); Collections.shuffle(vec07); System.out.println("After shuffling we get the set here ---->: " + vec07); } }
Original Vector is here ----> : [10, 16, 7, 2001, 1997] After shuffling we get the set here ---->: [1997, 10, 7, 16, 2001]The Chinese translation of
import java.util.*; import java.util.Vector; import java.util.Collections; public class ARBRDD { public static void main(String[] args){ Vector<String> vec = new Vector<String>(); vec.add("13109"); vec.add("KOAA-DHAKA Maitree Express"); vec.add("International Railway Connectivity"); vec.add("India"); vec.add("Bangladesh"); System.out.println("Original Vector is here ----> : " + vec); Collections.shuffle(vec, new Random()); System.out.println("\nShuffled Vector with Random() is here ----> : \n" + vec); Collections.shuffle(vec, new Random(3)); System.out.println("\nShuffled Vector with Random(3) is here ---->: \n" + vec); Collections.shuffle(vec, new Random(5)); System.out.println("\nShuffled Vector with Random(5) is here ----> : \n" + vec); } }
Original Vector is here ----> : [13109, KOAA-DHAKA Maitree Express, International Railway Connectivity, India, Bangladesh] Shuffled Vector with Random() is here ----> : [KOAA-DHAKA Maitree Express, 13109, International Railway Connectivity, India, Bangladesh] Shuffled Vector with Random(3) is here ---->: [India, 13109, KOAA-DHAKA Maitree Express, International Railway Connectivity, Bangladesh] Shuffled Vector with Random(5) is here ----> : [International Railway Connectivity, 13109, Bangladesh, India, KOAA-DHAKA Maitree Express]The Chinese translation of
import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Random; public class RandomizeList { public static void main(String args[]) { List<Integer> numbers = Arrays.asList(16, 7, 10, 2001, 1997, 10, 2022); System.out.println("Particular List Before Shuffling Present Here ---->: " + numbers); Collections.shuffle(numbers); System.out.println("The Particular List after shuffling Done --->: " + numbers); Collections.shuffle(numbers, new Random(System.nanoTime())); System.out.println("Particular List After Shuffling Again Done ---->: " + numbers); } }
Particular List Before Shuffling Present Here ---->: [16, 7, 10, 2001, 1997, 10, 2022] The Particular List after shuffling Done --->: [1997, 2001, 10, 2022, 7, 10, 16] Particular List After Shuffling Again Done ---->: [1997, 2022, 10, 10, 16, 7, 2001]
import java.util.*; public class Greetingsshufflelist { public static void main(String[] args) { List<String> list = Arrays.asList("Hi!", "Hello!", "Hallo!", "Bonjour!"); System.out.println(list); Collections.shuffle(list, new Random(7)); System.out.println(list); } }
[Hi!, Hello!, Hallo!, Bonjour!] [Hi!, Hello!, Bonjour!, Hallo!]
Fisher Yates shuffle algorithm is a hypothetical process running method in Java with a running complexity of O(n). The rand() function generates a random number in O(1) time.
import java.util.Random; import java.util.Arrays; public class ShuffleRand{ static void randomize( int arr[], int n){ Random r = new Random(); for (int i = n-1; i > 0; i--) { int j = r.nextInt(i+1); int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } System.out.println(Arrays.toString(arr)); } public static void main(String[] args){ int[] arr = {16, 7, 10, 2022, 1997, 2001, 25, 11}; int n = arr.length; randomize (arr, n); } }
[1997, 2022, 2001, 25, 11, 16, 7, 10]
From today's article, we learned about some possible shuffle methods of java code through syntax and algorithms. Hopefully this article helped you understand how the various vector shuffling methods mentioned here operate.
The above is the detailed content of Java program to shuffle vector elements. For more information, please follow other related articles on the PHP Chinese website!