Java 具有多个参数构造函数的选择排序

Java 具有多个参数构造函数的选择排序,java,Java,我正在我的大学里为一个CS140课程做一个项目,我正在尝试创建一个使用选择排序的程序,该程序使用美国50个州的数组,其中包含名称、人口、首都和地区字段。以下是给定的数组: private static void loadStates() { // Set the search key to be the state name. State.setKey("name"); us = new Country("United States", 311591917, "Wash

我正在我的大学里为一个CS140课程做一个项目,我正在尝试创建一个使用选择排序的程序,该程序使用美国50个州的数组,其中包含名称、人口、首都和地区字段。以下是给定的数组:

private static void loadStates() {
    // Set the search key to be the state name.
    State.setKey("name");

    us = new Country("United States", 311591917, "Washington DC",
            new State[]{
                new State("Alabama", 4802740, "Montgomery", "Southeast"),
                new State("Alaska", 722718, "Juneau", "West"),
                new State("Arizona", 6482505, "Phoenix", "Southwest"),
                new State("Arkansas", 2937979, "Little Rock", "Southeast"),
                new State("California", 37691912, "Sacramento", "West"),
                new State("Colorado", 5116769, "Denver", "West"),
                new State("Connecticut", 3580709, "Hartford", "Northeast"),
                new State("Delaware", 907135, "Dover", "Northeast"),
                new State("Florida", 19057542, "Tallahassee", "Southeast"),
                new State("Georgia", 9815210, "Atlanta", "Southeast"),
                new State("Hawaii", 1374810, "Honolulu", "West"),
                new State("Idaho", 1584985, "Boise", "West"),
                new State("Illinois", 12869257, "Springfield", "Midwest"),
                new State("Indiana", 6516922, "Indianapolis", "Midwest"),
                new State("Iowa", 3062309, "Des Moines", "Midwest"),
                new State("Kansas", 2871238, "Topeka", "Midwest"),
                new State("Kentucky", 4369356, "Frankfurt", "Southeast"),
                new State("Louisiana", 4574836, "Baton Rouge", "Southeast"),
                new State("Maine", 1328188, "Augusta", "Northeast"),
                new State("Maryland", 5828289, "Annapolis", "Northeast"),
                new State("Massachusetts", 6587536, "Boston", "Northeast"),
                new State("Michigan", 9876187, "Lansing", "Midwest"),
                new State("Minnesota", 5344861, "St. Paul", "Midwest"),
                new State("Mississippi", 2978512, "Jackson", "Southeast"),
                new State("Missouri", 6010688, "Jefferson City", "Midwest"),
                new State("Montana", 998199, "Helena", "West"),
                new State("Nebraska", 1842641, "Lincoln", "Midwest"),
                new State("Nevada", 2723322, "Carson City", "West"),
                new State("New Hampshire", 1318194, "Concord", "Northeast"),
                new State("New Jersey", 8821155, "Trenton", "Northeast"),
                new State("New Mexico", 2082224, "Santa Fe", "Southwest"),
                new State("New York", 19465197, "Albany", "Northeast"),
                new State("North Carolina", 9656401, "Raleigh", "Southeast"),
                new State("North Dakota", 683932, "Bismarck", "Midwest"),
                new State("Ohio", 11544951, "Columbus", "Midwest"),
                new State("Oklahoma", 3791508, "Oklahoma City", "Southwest"),
                new State("Oregon", 3871859, "Salem", "West"),
                new State("Pennsylvania", 12742886, "Harrisburg", "Northeast"),
                new State("Rhode Island", 1051302, "Providence", "Northeast"),
                new State("South Carolina", 4679230, "Columbia", "Southeast"),
                new State("South Dakota", 824082, "Pierre", "Midwest"),
                new State("Tennessee", 6403353, "Nashville", "Southeast"),
                new State("Texas", 25674681, "Austin", "Southwest"),
                new State("Utah", 2817222, "Salt Lake City", "West"),
                new State("Vermont", 4802740, "Montpelier", "Northeast"),
                new State("Virginia", 8096604, "Richmond", "Southeast"),
                new State("Washington", 6830038, "Olympia", "West"),
                new State("West Virginia", 1855364, "Charleston", "Southeast"),
                new State("Wisconsin", 5711767, "Madison", "Midwest"),
                new State("Wyoming", 568158, "Cheyenne", "West")
            });
} // end loadStates()
每个州都是如此。以下是我的选择排序。根据教授的要求分为三种方法

private static void selectionSort(State[] states, String onField) {
    for (int top = 0; top < states.length - 1; top++) {
        swap(states, top, indexOfMinValueInArray(states, top, onField));
    }
} // end selectionSort()

private static int indexOfMinValueInArray(State[] states, int startAt,
        String onField) {
    int minIndex = startAt;
    if ("name".equals(onField)) {
        for (int index = startAt; index < states.length; index++) {
            if (states[index].getName().compareTo(states[minIndex].getName()) < 0) {
                minIndex = index;
            }
        }
    } else if ("population".equals(onField)) {
        for (int index = startAt; index < states.length; index++) {
            if (states[index].getPopulation() < states[minIndex].getPopulation()) {
                minIndex = index;
            }
        }
    }
    return minIndex;
}// end indexOfMinValueInArray()

private static void swap(State[] states, int index1, int index2) {
    State temp = states[index1];
    states[index1] = states[index2];
    states[index2] = temp;
}
private static void selectionSort(State[]states,String onField){
对于(int-top=0;top
基本上,这应该做的是获取一个“键”,或者,如方法中所述,“onField”,它应该告诉选择排序按名称、人口、资本或地区对数组进行排序。这就是我迷路的地方。有人能提供一些关于如何更正我的选择排序的指导吗?另外,这里是代码的开头,所以您可以看到我需要采取的方向

public static void main(String[] args) {
    loadStates();

    State[] sortedStates = new State[50];
    for (int i = 0; i <= 49; i++) {
        sortedStates[i] = us.getStateAtIndex(i);
    }

    // TODO: Sort the states by population.
    selectionSort(sortedStates, "population");

    // TODO: List the states by population.
    listStates(sortedStates);
publicstaticvoidmain(字符串[]args){
loadStates();
State[]sortedStates=新状态[50];
对于(int i=0;i我建议定义四个对象,每个字段一个作为键。例如,对于
name
字段:

public class NameComparator implements Comparator<State> {
    public int compare(State s1, State s2) {
        return s1.name.compareTo(s2.name);
    }
}
公共类NameComparator实现Comparator{
公共整数比较(状态s1、状态s2){
返回s1.name.compareTo(s2.name);
}
}

其他比较器可以类似地定义。然后,您可以定义选择排序以使用
比较器来进行项目比较,并且可以根据键名称选择要使用的比较器。

if(states[index].getName().compareTo(states[minIndex].getName())<0
这是否正确?是的,我已经在那里实现了类似的东西,比如nair.ashvin刚才的评论。根据我的理解,数组没有显示selectionSort(sortedStates,“population”);ListState(sortedStates)@DarthCthulhu-列表根本不显示?你的意思是你只是在
ListState
上有问题吗?如果是这样,很容易解决:
for(State-State:sortedStates){/*打印状态*/}
好的,在尝试之后,我得到了要显示的状态,但它们没有被排序。这显然意味着选择排序有问题?我想我没有正确设置它来检测我是否要按名称、人口、首都或地区排序。@DarthCthulhu-它在任何键上排序正确吗?如果没有,这是你的排序算法。你能把所有的州都粘贴到我们这里吗?我刚刚检查了你的代码,它工作得很好。对我来说唯一的区别是我使用了(州s:sortedStates)System.out.println(s)的
而不是
listStates
nair.ashvin,我继续添加了原始帖子中给出的整个数组/方法。这是教授给我的格式。另外,我添加了for语句,它打印出来,但不排序。因此,这告诉我排序算法有问题。我想我没有很奇怪,我复制粘贴了你的代码,它对我有效,当我要求它时,它可以按人口和名称正确地对州进行排序。我不明白这怎么对你有效。你需要州和国家的java文件,构造函数才能正常工作怎么了。。?