- package com.heima.demo;
- import java.util.ArrayList;
- import java.util.Random;
- public class Demo {
- public static void main(String[] args) {
- int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
- ArrayList<Integer> al = new ArrayList<>();
- Random r = new Random();
- int index;
- for(int x = 0; x < 3; x++) {
- index = r.nextInt(arr.length); //数组角标索引 [0, 10)
- if(al.contains(index)) {
- x--;
- }else {
- al.add(index);
- System.out.println(arr[index]);
- }
- }
- System.out.println("---------------------");
- for(int x = 0; x < 3; x++) {
- index = r.nextInt(arr.length); //[0, 10)
- if(al.contains(index)) {
- x--;
- }else {
- al.add(index);
- System.out.println(arr[index]);
- }
- }
- }
- }
复制代码 |