Home > Java > javaTutorial > Java program to shuffle vector elements

Java program to shuffle vector elements

WBOY
Release: 2023-08-27 09:05:05
forward
545 people have browsed it

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.

Write a Java program to scramble the algorithm of vector elements

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.

Syntax for writing a Java program to shuffle vector elements

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);  
   }  
}  
Copy after login

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.

How to build a Java program to scramble vector elements

  • Method 1 - Java program to shuffle vector elements

  • Method 2−Fisher-Yates Shuffling Algorithm

Java program to scramble vector elements

Here we include some Java build code that allows us to shuffle some vector elements in a simple and random way.

The Chinese translation of

Example 1

is:

Example 1

import 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);
   }
}
Copy after login

Output

Vector contains are present in the list:----> [16, 2001, 7, 10, 1997]
Copy after login

Example 2

is translated as:

Example 2

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);
	}
}
Copy after login

Output

Original Vector is here ----> : [10, 16, 7, 2001, 1997]
After shuffling we get the set here ---->: [1997, 10, 7, 16, 2001]
Copy after login
The Chinese translation of

Example 3

is:

Example 3

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);
	}
}
Copy after login

Output

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]
Copy after login
The Chinese translation of

Example 4

is:

Example 4

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); 
   }
}
Copy after login

Output

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]
Copy after login

Example 5

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);         
   }  
}
Copy after login

Output

[Hi!, Hello!, Hallo!, Bonjour!]
[Hi!, Hello!, Bonjour!, Hallo!]
Copy after login

Fisher-Yates shuffling algorithm shuffles vector elements

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.

Example 6

is translated as:

Example 6

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);
	}
}
Copy after login

Output

[1997, 2022, 2001, 25, 11, 16, 7, 10]
Copy after login

in conclusion

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template