【发布时间】:2015-05-22 18:09:44
【问题描述】:
。基本上,以下代码的作用(假设)是,创建一组非重复随机数,将它们填充到一个数组中,该数组将转换为一个列表,然后对它们进行排序。问题是嵌套的 for 循环,我设法解决了问题,但甚至不确定它是如何工作的。其次,我似乎无法正确排序,事情会重复出现,并且不时弹出超出范围的错误。
代码的工作原理:
- 生成不重复的随机数
- 用它们填充数组
- 使用嵌套 for 循环查找最小值
- 将其插入新数组
- 从第一个数组中删除它
-
重复最后两个步骤,直到第一个数组为空,第二个数组为空 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; } }
【问题讨论】:
-
while (ArrayUtils.contains(holdArray, rand)) { rand = randomNum(0, 20); } 你没有使用你的界限。
-
分而治之。首先编写一个简单的排序函数并对其进行测试。您不想使用内置的排序方法来学习对吗?阅读冒泡排序或类似的东西并首先实现它。那么剩下的就容易多了。
-
我不明白。您有四个不同的
Integer[]数组,三个int[]数组和两个ArrayLists。其中一些是原始数组的副本,一些用nulls 或零填充(它们都不是空的)。目前尚不清楚您在排序过程中实际阅读的是哪个,但很明显,附加到非空列表无法产生正确的结果。