java
package com.mohuanan.exercise;
import java.util.Random;
//打乱数组的元素
public class ExerciseDemo05 {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
Random r = new Random();
for (int i = 0; i < arr.length; i++) {
int randomIndex = r.nextInt(5);
int temp;//临时变量
temp = arr[randomIndex];
arr[randomIndex] = arr[i];
arr[i] = temp;
}
//遍历数组,并输出
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+"");
}
}
}