Java排序循环不工作

标签 java arrays list sorting

。基本上,以下代码的作用(假设)是创建一组不重复的随机数,将它们填充到一个数组中,该数组将转换为列表,然后对它们进行排序。问题是嵌套的 for 循环,我设法解决了这个问题,但甚至不确定它是如何工作的。其次,我似乎无法正确排序,事情重复并且不时弹出越界错误。

代码的工作原理:

  1. 生成不重复的随机数
  2. 用它们填充数组
  3. 使用嵌套for循环查找最小值
  4. 将其插入新数组
  5. 将其从第一个数组中删除
  6. 重复最后 2 个步骤,直到第一个数组为空且第二个数组为空 操作系统按排序顺序填写

     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;
    
    }
    
    
    
    }
    

最佳答案

你能给出一个令人信服的理由来证明从数组转换为列表的合理性吗?为什么不完全只使用列表或仅使用数组?我在下面的回答中使用了 ArrayList;

首先是 fillArray 类。您不需要用全零填充。为什么还要费心用一个您无论如何都会替换的值来填充它呢?

public static ArrayList<Integer> fillArray(int size,int lowerBound,int upperBound){
    ArrayList<Integer> a = new ArrayList<Integer>(size);
    for (int count =0;count < size;count++){
       Integer rand = new Integer(randomNum(lowerBound,upperBound));
       a.add(rand);
    }
    return a;
}

第二,排序类。正如你所说,你的方法是寻找最低值,然后做一些神奇的事情等等。

public static ArrayList<Integer> sorter(ArrayList<Integer> unsorted) {
ArrayList<Integer> sortedArray = new ArrayList<Integer>(unsorted.size());
while(!unsorted.isEmpty()) { //repeats until the unsorted list is empty
    int minval = unsorted.get(0);
    int removeIndex = 0;
    for(int i=1;i<unsorted.size();i++) 
        if (unsorted.get(i)<minval) {
            minval = unsorted.get(i);
            removeIndex = i;
        }
    sortedArray.add(minval);
    unsorted.remove(removeIndex);
    }
    return sortedArray;
}

测试的主要方法

public static void main(String[] args) {
    ArrayList<Integer> a = fillArray(20,1,25);
    System.out.println("unsorted array");
    for (Integer c : a)
        System.out.print(c + ";");
    ArrayList<Integer> b = sorter(a);
    System.out.println("\nnew unsorted array");
    for (Integer c : a)
        System.out.print(c + ";");
    System.out.println("\nsorted array");
    for (Integer c : b)
        System.out.print(c + ";");
}

这个输出

unsorted array
22;2;23;22;13;12;4;1;7;14;25;18;9;12;3;8;20;3;1;20;
new unsorted array

sorted array
1;1;2;3;3;4;7;8;9;12;12;13;14;18;20;20;22;22;23;25;

关于Java排序循环不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30403235/

相关文章:

java - Dagger 在注入(inject)嵌套依赖项时抛出 IllegalStateException

java - 从字符串对象数组中选择排序字符串而不导入除 Scanner 之外的任何内容(提供代码)?

javascript - 由 ng-repeat : collect states under one array in the $scope model? 创建的按钮列表

c# - 针对列表中的每个其他项目对列表中的每个项目执行操作,并排除

java - 从 Groovy 中的 List<WebElement> 获取文本

javascript - 无循环地为数组中的每个对象赋值

c - 将结构体数组作为指针传递给函数 (C)

r - 与 data.frame 长度不等的字符向量列表

java - 在 Java 8 中用另一个列表过滤对象列表的列表

java - Gradle 导入 Powermock 和 Mockito