。基本上下面的代碼所做的(假設)是,創建一組非重複的隨機數,將它們填充到一個數組中,該數組被轉換爲一個列表並將其排序。問題是嵌套for循環,我管理一個工作,但不知道它是如何工作的。經常,我似乎無法正確排序,重複出現的情況以及不時出現的界限錯誤。Java排序循環不起作用
如何代碼工作:
- 生成不重複的隨機數
- 裝滿他們的陣列
- 使用嵌套的循環來找到最小值
- 請插入到一個新的數組
- 將其從第一個陣列中刪除
重複最後2個步驟,直到第一個數組爲空,第二個陣列 OS填充在排序順序
import org.apache.commons.lang.ArrayUtils; import java.util.ArrayList; import java.util.Arrays; import java.util.*; import java.lang.*; import java.io.*; public class Sorter { public static void main(String[] args) { int[] process = fillArray(20,1,25); sorter(process,20); } public static int[] sorter(int array[],int size) { int[] useArray = array; Integer[] newArray = ArrayUtils.toObject(useArray); List<Integer> arrayList = new ArrayList(Arrays.asList(newArray)); //System.out.println((arrayList)); int counter = 1; int minval = 0; int diffsize = size - 1; int actualVal = 0; int storeArray[] = new int[size]; int removeIndex =0; Integer[] newStore = ArrayUtils.toObject(storeArray); List<Integer> storeList = new ArrayList(Arrays.asList(newStore)); System.out.println((arrayList)); // Both loops messed up for (int i = 0; i < size+diffsize; i++) { for (int n = 0; n < size-1; n++) { if (arrayList.get(minval) < arrayList.get(counter)) { actualVal = arrayList.get(minval); System.out.println((arrayList.get(minval)) + " Less than " + arrayList.get(counter)); counter = counter + 1; removeIndex = minval; } else { actualVal = arrayList.get(counter); System.out.println((arrayList.get(counter)) + " Less than " + arrayList.get(minval)); minval = counter; counter = counter + 1; removeIndex = counter; } } // System.out.println(actualVal); storeList.add(actualVal); arrayList.remove(actualVal); // need to remove the smallest value to repeat the sorting and get the next smallest value, but this is not removing it size = size - 1; counter = 1; minval = 0; // if (i + size == i) { // storeList.set(i, arrayList.get(0)); // } // System.out.println(removeIndex); // System.out.println(arrayList); } // System.out.println(storeList); int[] ints = new int[storeList.size()]; int d = 0; for (Integer u : storeList) { ints[d++] = u; } return ints; } public static int randomNum(int lower,int upper){ Random rand = new Random(); int randomNum = lower + rand.nextInt((upper- lower) + 1); return randomNum; } public static int[] fillArray(int size,int lowerBound,int upperBound){ int holdArray[] = new int[size]; int rand = 0; for (int count =0;count < holdArray.length;count++){ holdArray[count] = 0; } for (int count =0;count < holdArray.length;count++){ rand = randomNum(lowerBound,upperBound); if (ArrayUtils.contains(holdArray, rand)) { while (ArrayUtils.contains(holdArray, rand)) { rand = randomNum(0, 20); } } holdArray[count] = rand; } // System.out.println(Arrays.toString(holdArray)); //return holdArray; return holdArray; } }
而(ArrayUtils.contains(holdArray,RAND)){ 蘭特= randomNum(0,20); }你沒有使用你的界限。 –
分而治之。先寫一個簡單的排序函數並測試一下。你不想使用內置的排序方法,所以你可以學習對嗎?閱讀泡沫排序或類似的東西,並首先實現。然後其餘的會更容易。 –
我不明白。你有四個不同的'Integer []'數組,三個'int []'數組*和*兩個'ArrayList's。其中一些是原始數組的副本,其中一些填充了'null'或零(它們都不爲空)。目前還不清楚在排序過程中您實際正在閱讀的內容,但很明顯,附加到非空列表無法產生正確的結果。 – Holger