所以我開始了一個方法,它應該給我一個充滿隨機非重復值的陣列,這些值也很奇怪。但是,我只讓陣列填充了一些錯誤的值:
public static int[] Array(int n, int max) {
int [] arr = new int [n];
int newnum;
Random rn = new Random();
for(int i = 0; i < arr.length; i ){
newnum = rn.nextInt(0,max);
for(int j = 0; j < i; j ){
while(newnum == arr[j] && newnum % 2 == 0){
newnum = rn.nextInt(0, max);
}
}
arr[i] = newnum;
}
return arr;
}
uj5u.com熱心網友回復:
這里的問題是:
- 您遍歷串列中已有的所有數字,如果它等于 j,則重新滾動您的數字。但是,一旦您完成比較,您的新數字可能等于 arr[j-1],您無需重新檢查。每次有比賽時,您都需要重新開始。
- 應該是|| 而不是 && 在你的情況下。如果數字等于前一個數字或如果它是偶數,您想重新滾動。
優化也很糟糕,但我想這不是你問的。
固定版本(但未優化):
public static int[] array(int n, int max) {
int[] arr = new int[n];
int newnum;
Random rn = new Random();
for (int i = 0; i < arr.length; i ) {
newnum = rn.nextInt(max);
for (int j = 0; j < i; j ) {
if (newnum == arr[j] || newnum % 2 == 0) {
//or instead of and
//we are also using if here
//because we are already re-running the loop by starting over from j = 0
newnum = rn.nextInt(max);
j = 0;
}
}
arr[i] = newnum;
}
return arr;
}
使用 java 特性有更好的方法來做到這一點,但我想你還在上學,他們還沒有教他們。
uj5u.com熱心網友回復:
為什么不創建一個集合并繼續添加直到你有 n 個元素?
uj5u.com熱心網友回復:
您應該確保將代碼布置好以便您理解它。
您應該確保您的條件始終正確,并且毫不猶豫地將復雜性委托給其他方法。
public static int[] array(int n, int max) {
int[] arr = new int[n];
Random rn = new Random();
for (int i = 0; i < arr.length; i ) {
int candidate = rn.nextInt(max);
while (candidate % 2 == 0 || isContainedIn(candidate, arr, i - 1)) {
candidate = rn.nextInt(max);
}
arr[i] = candidate;
}
return arr;
}
private static boolean isContainedIn(int candidate, int[] arr, int index) {
for (int i = 0; i <= index; i ) {
if (arr[i] == candidate) {
return true;
}
}
return false;
}
另一種方法是使用流:
public static int[] array(int n, int max) {
max /= 2; // Divide by two to avoid generating twice the expected numbers.
if (n > max) throw new IllegalArgumentException("Impossible to generate such an array");
int[] array = new Random().ints(0, max) // Generate ints between 0 and half max
.map(i -> i * 2 1) // Make sure they're odds
.distinct() // Make sure they don't repeat
.limit(n) // Take n of those numbers
.toArray(); // Put them in an array
return array;
}
uj5u.com熱心網友回復:
您想要一個范圍內的非重復奇數。對于非重復數字,您最好使用小范圍的隨機播放或大范圍的集合。
對于選擇奇數,可能更容易從范圍的一半中選擇整數并使用num = 2 * pick 1以便不生成任何偶數,盡管您需要小心為初始選擇設定正確的整數范圍。
uj5u.com熱心網友回復:
正如 Janardhan Maithil 建議的那樣,您可以使用一組來確定命運
但是您還必須考慮如果嘗試獲得比可用值更多的值會發生什么。
public static int[] randomDistinctiveOdds(int maxAmount, int maxValue) {
if (maxAmount / 2 > maxValue) {
throw new IllegalArgumentException("Not enough distinct odd values in the range from 0 to " maxValue);
}
Random random = new Random();
Set<Integer> result = new HashSet<>(maxAmount);
int nextInt;
while (result.size() < maxAmount) {
if ((nextInt = random.nextInt(maxValue)) % 2 == 1)
result.add(nextInt);
}
return result.stream().mapToInt(Integer::intValue).toArray();
}
或帶有流的解決方案
public static int[] randomDistinctiveOddsWithStreams(int maxAmount, int maxValue) {
if (maxAmount / 2 > maxValue) {
throw new IllegalArgumentException("Not enough distinct odd values in the range from 0 to " maxValue);
}
List<Integer> odds = IntStream.range(0, maxValue)
.boxed()
.filter(i -> i % 2 == 1) // only odds
.collect(Collectors.toList());
Collections.shuffle(odds);
return odds.stream()
.mapToInt(Integer::intValue)
.limit(maxAmount) // take at most maxAmount
.toArray();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/467429.html
