数组为int[ ]类型,包含10个元素,这些元素为10到30之间的随机数,且均不重复,请问如何实现?
代码实现:
import java.util.Random;
public class RandomBornTool {
public static int[] randomArrayBornTool(int startNumber, int endNumber,
int length) {
int[] arr = new int[length];
Random r = new Random();
arr[0] = r.nextInt(endNumber - startNumber+1) + startNumber;
int m = 1;
while (m < length) {
int temp = r.nextInt(endNumber - startNumber+1) + startNumber;
boolean flag = true;
for (int n = 0; n < m; n++) {
flag = (flag && (arr[n] != temp));
}
if (flag) {
arr[m] = temp;
m++;
}
}
return arr;
}
} |
|