[英]Trouble sorting numbers from smallest to largest in an array
This is one of my first posts on here so I am not sure if I am posting this correctly. 这是我在此处发布的第一篇文章之一,因此不确定我是否正确张贴了这篇文章。 But I need help trying to put the population of the states in order from smallest to largest that comes from a separate file. 但是我需要帮助,试图将来自单独文件的州的人口从最小到最大按顺序排列。 All the program is outputting is the states in alphabetical order. 程序输出的所有状态均为字母顺序。 The file is set up like below. 该文件的设置如下。
Expected input: 预期输入:
Alabama,4779736
Alaska,710231
Arizona,6392017
Class that attempts to sort: 尝试排序的类:
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;
}
}
}
}
}
Your program already is sorting the array in ascending order of population. 您的程序已经在按人口的升序对数组进行排序。
You are just not seeing it. 您只是没有看到它。
In your main, you print the arrays and then sort: 在您的主目录中,打印数组,然后排序:
printarray (myarray, fillsize, prw);
sortarray(myarray, statearray, fillsize);
Instead, try sorting and only then printing: 而是尝试排序,然后再打印:
sortarray(myarray, statearray, fillsize);
printarray (myarray, fillsize, prw);
You'll see your program is correct. 您会看到您的程序是正确的。
Tip: You can use System.out.println(Arrays.toString(myarray));
提示:您可以使用System.out.println(Arrays.toString(myarray));
to print arrays easily. 轻松打印数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.