package Base_se.Base_701;
import java.util.Random;
/**
* @author gyf
* @ClassName test
* @Date 2024/7/1 19:57
* @Version V1.0
* @Description : 打乱数组中所有数据的顺序
*/
public class Solution {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
Random random = new Random();
for (int i = 0; i < arr.length; i++) {
// 生成随机索引
int random_index = random.nextInt(arr.length);
System.out.println(random_index);
// 随机索引指向元素和i指向元素交换
int temp = arr[i];
arr[i] = arr[random_index];
arr[random_index] = temp;
}
for (int i = 0; i < arr.length; i++) {
System.out.println();
System.out.println(arr[i]);
}
}
}
打乱数组中所有数据的顺序
于 2024-07-02 10:13:57 首次发布