Java 无法将数组中的数字从最小值排序到最大值

Java 无法将数组中的数字从最小值排序到最大值,java,arrays,Java,Arrays,这是我在这里的第一篇帖子,所以我不确定我是否发布了正确的帖子。但我需要帮助,试图把各州的人口从最小到最大,从一个单独的文件中排序。所有程序输出的是按字母顺序排列的状态。文件的设置如下所示 预期输入: Alabama,4779736 Alaska,710231 Arizona,6392017 public class Inorder { /** * @param args * @throws IOException */ public stat

这是我在这里的第一篇帖子,所以我不确定我是否发布了正确的帖子。但我需要帮助,试图把各州的人口从最小到最大,从一个单独的文件中排序。所有程序输出的是按字母顺序排列的状态。文件的设置如下所示

预期输入:

Alabama,4779736
Alaska,710231
Arizona,6392017  
public class Inorder {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        PrintWriter prw = new PrintWriter("outfile.txt");
        File f = new File("census2010.txt");
        if (!f.exists()) {
            System.out.println("f does not exist ");
        }
        Scanner infile = new Scanner(f);
        infile.useDelimiter("[\t|,|\n|\r]+");
        final int MAX = 50;
        int[] myarray = new int[MAX];
        String[] statearray = new String[MAX];
        int fillsize;

        fillsize = fillarray(myarray, statearray, infile);

        printarray(myarray, fillsize, prw);
        sortarray(myarray, statearray, fillsize);

    }

    public static int fillarray(int[] num, String[] states, Scanner infile) {

        int retcnt = 0;
        for (int count = 0; count < 50; count++) {

            int pop;
            String state;
            state = infile.next();
            pop = infile.nextInt();
            System.out.println(state + " " + pop + " ");
            states[retcnt] = state;
            num[retcnt] = pop;
            retcnt++;

        }
        return (retcnt);

    }

    public static void printarray(int[] num, int fillsize, PrintWriter prw) {
        for (int counts = 0; counts < fillsize; counts++) {
            System.out.println("For the position [" + counts
                    + "] the value is " + num[counts]);
            prw.println("For the position [" + counts + "] the value is "
                    + num[counts]);
        }
        return;
    }

    public static void sortarray(int[] poparray, String[] statearray,
            int fillsize) {

        for (int fill = 0; fill < fillsize - 1; fill = fill + 1) {

            for (int compare = fill + 1; compare < fillsize; compare++) {

                if (poparray[compare] < poparray[fill]) {
                    int poptemp = poparray[fill];
                    poparray[fill] = poparray[compare];
                    poparray[compare] = poptemp;
                    // do I need something here?
                    String statetemp = statearray[fill];
                    statearray[fill] = statearray[compare];
                    statearray[compare] = statetemp;

                }
            }
        }
    }
}
尝试排序的类:

Alabama,4779736
Alaska,710231
Arizona,6392017  
public class Inorder {

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        PrintWriter prw = new PrintWriter("outfile.txt");
        File f = new File("census2010.txt");
        if (!f.exists()) {
            System.out.println("f does not exist ");
        }
        Scanner infile = new Scanner(f);
        infile.useDelimiter("[\t|,|\n|\r]+");
        final int MAX = 50;
        int[] myarray = new int[MAX];
        String[] statearray = new String[MAX];
        int fillsize;

        fillsize = fillarray(myarray, statearray, infile);

        printarray(myarray, fillsize, prw);
        sortarray(myarray, statearray, fillsize);

    }

    public static int fillarray(int[] num, String[] states, Scanner infile) {

        int retcnt = 0;
        for (int count = 0; count < 50; count++) {

            int pop;
            String state;
            state = infile.next();
            pop = infile.nextInt();
            System.out.println(state + " " + pop + " ");
            states[retcnt] = state;
            num[retcnt] = pop;
            retcnt++;

        }
        return (retcnt);

    }

    public static void printarray(int[] num, int fillsize, PrintWriter prw) {
        for (int counts = 0; counts < fillsize; counts++) {
            System.out.println("For the position [" + counts
                    + "] the value is " + num[counts]);
            prw.println("For the position [" + counts + "] the value is "
                    + num[counts]);
        }
        return;
    }

    public static void sortarray(int[] poparray, String[] statearray,
            int fillsize) {

        for (int fill = 0; fill < fillsize - 1; fill = fill + 1) {

            for (int compare = fill + 1; compare < fillsize; compare++) {

                if (poparray[compare] < poparray[fill]) {
                    int poptemp = poparray[fill];
                    poparray[fill] = poparray[compare];
                    poparray[compare] = poptemp;
                    // do I need something here?
                    String statetemp = statearray[fill];
                    statearray[fill] = statearray[compare];
                    statearray[compare] = statetemp;

                }
            }
        }
    }
}
公共类顺序{
/**
*@param args
*@抛出异常
*/
公共静态void main(字符串[]args)引发IOException{
//TODO自动生成的方法存根
PrintWriter prw=新的PrintWriter(“outfile.txt”);
文件f=新文件(“census2010.txt”);
如果(!f.exists()){
System.out.println(“f不存在”);
}
扫描仪填充=新扫描仪(f);
infle.useDelimiter(“[\t |,|\n |\r]+”;
最终int最大值=50;
int[]myarray=新的int[MAX];
String[]statearray=新字符串[MAX];
int fillsize;
fillsize=fillarray(myarray、statearray、infle);
printarray(myarray、fillsize、prw);
sortarray(myarray、statearray、fillsize);
}
公共静态int-fillarray(int[]num,String[]states,Scanner-infle){
int-retcnt=0;
用于(int count=0;count<50;count++){
int-pop;
字符串状态;
state=infle.next();
pop=infle.nextInt();
System.out.println(状态+“”+pop+“”);
状态[retcnt]=状态;
num[retcnt]=pop;
retcnt++;
}
返回(retcnt);
}
公共静态void printary(int[]num,int fillsize,PrintWriter prw){
对于(int counts=0;counts
您的程序已经在按人口的升序对数组进行排序

你只是看不到而已

在main中,打印阵列,然后进行排序:

printarray (myarray, fillsize, prw);
sortarray(myarray, statearray, fillsize);
相反,请尝试排序,然后再打印:

sortarray(myarray, statearray, fillsize);
printarray (myarray, fillsize, prw);
你会发现你的程序是正确的

提示:
您可以使用
System.out.println(Arrays.toString(myarray))
可以轻松打印数组。

您不应该有两个数组,而应该只有一个数组,其中包含状态对象的实例,具有名称和填充。然后可以使用Arrays.sort()使用标准算法对该数组进行排序,而无需编写自己的算法。还要学习Java命名约定并坚持它们。您的比较标准是
poparray[compare]
。这是你认为的排序吗?我相信我的问题是排序数组。我的教授说我们需要使用两个数组。我不应该使用两个数组吗?也许你可以和一个同学组成一个学习小组?就这些!!我只需要把sortarray移到打印阵列上。非常感谢。我会投你一票,但我不能,因为我是个新手,没问题!我很高兴能帮上忙!