- package day03;
- public class Text {
- public static void main(String[] args) {
- int[] myList = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- // System.out.println(myList.length);
- for (int i = 0; i < myList.length; i++) {
- int index = (int) (Math.random() * myList.length);
- int temp = myList[i];
- myList[i] = myList[index];
- myList[index] = temp;
- }
- System.out.print("[");
- for (int i = 0; i < myList.length; i++) {
- if (i != myList.length - 1)
- System.out.print(myList[i] + " ");
- else
- System.out.println(myList[i] + "]");
- }
- }
- }
复制代码 把代码补充一下,以上代码运行结果是没错的,能将1-9重新排序,但是Math.random()导致重复的问题,不会产生影响吗? |