Write a Java program that implements the functionality of the Python shuffle() function,…
- User
Write a Java program that implements the functionality of the Python shuffle() function, which shuffles the elements of a given sequence as call parameter. The Java method that performs the shuffle should be generic, allowing the use of any component type of the given sequence, e.g., arrays of: integers, floating-point numbers, strings, or characters.
Assistant
Below is a Java program that implements a generic method to shuffle the elements of an array. The method uses the Fisher-Yates shuffle algorithm, which is an efficient and unbiased way to shuffle elements.
import java.util.Random; public class ShuffleArray { // Generic method to shuffle an array public static <T> void shuffle(T[] array) { int n = array.length; Random random = new Random(); // Start from the last element and swap one by one for (int i = n - 1; i > 0; i--) { // Pick a random index from 0 to i int j = random.nextInt(i + 1); // Swap array[i] with the element at random index T temp = array[i]; array[i] = array[j]; array[j] = temp; } } // Example usage public static void main(String[] args) { // Shuffle an array of integers Integer[] intArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; shuffle(intArray); System.out.println("Shuffled Integer Array: "); printArray(intArray); // Shuffle an array of strings String[] stringArray = {"Apple", "Banana", "Cherry", "Date", "Elderberry"}; shuffle(stringArray); System.out.println("Shuffled String Array: "); printArray(stringArray); // Shuffle an array of characters Character[] charArray = {'a', 'b', 'c', 'd', 'e'}; shuffle(charArray); System.out.println("Shuffled Character Array: "); printArray(charArray); } // Generic method to print an array public static <T> void printArray(T[] array) { for (T element : array) { System.out.print(element + " "); } System.out.println(); } }Explanation:
-
Generic Method
shuffle:- The method
shuffleis generic, meaning it can work with arrays of any type (T[]). - It uses the Fisher-Yates shuffle algorithm to randomly rearrange the elements of the array.
- The algorithm iterates from the end of the array to the beginning, swapping each element with a randomly chosen element from the remaining unshuffled part of the array.
- The method
-
Random Number Generation:
Random random = new Random();initializes aRandomobject to generate random indices.random.nextInt(i + 1)generates a random index between0andi(inclusive).
-
Swapping Elements:
- The elements are swapped using a temporary variable
temp.
- The elements are swapped using a temporary variable
-
Example Usage:
- The
mainmethod demonstrates how to shuffle arrays of different types (integers, strings, and characters). - The
printArraymethod is a generic utility to print the elements of an array.
- The
This implementation ensures that the shuffle is unbiased and works efficiently for arrays of any type.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.