有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中使用数组内的选择排序

我有一个名为Person的类,其中有三个变量的构造函数、getter和setter:name、age和height。我还在这个类中实现了一个选择排序方法来对人的年龄进行排序。然后,我创建了一个包含10个人的数组,我给了他们不同的名字、年龄和身高,但我没有使用选择排序方法对人的年龄进行排序。我想知道您是否可以帮助我了解我做错了什么,以及为什么我没有在数组中使用该方法

我还想知道是否有一种更智能(更少人工)的方法来实现我想要的类型(包括姓名、年龄和身高)的数组,因为我将添加更多的人,比如20人,这将需要一些额外的工作,我想我可以用一些更好的方法来节省。我知道如何使用数组列表,但我想知道使用数组是否可行或合理

//Class Person

public class Person {
    private String name;
    private int height;
    private int age;

public void Person (String name, int height, int age){
    this.name = name;
    this.height = height;
    this.age = age;
}

public String getName (){
    return name;
}

public int getHeight (){
    return height;
}

public int getAge (){
    return age;
}

public void setName (String name) {
    this.name = name;
}

public void setHeight (int height) {
    this.height=height;
}

public void setAge (int age) {
    this.age=age;
}

public int[] selectionSort (int[] age){

    int i, j, minValue, minIndex, temp =0;

    for (i = 0; i<age.length; i++) {
        minValue = age[i];
        minIndex = i;

        for (j=i; j<age.length; j++) {

            if (age[i]<minValue){
                minValue = age [j];
                minIndex = j;
            }
        }
        if (minValue<age[i]){
            temp=age[i];
            age[i]=age[minIndex];
            age[minIndex]=temp;
        }
    }
return age;
}
}

//Array implementation

public class Main {

public static void main(String[] args) {

Person [] persons = new Person [3];

    persons [0] = new Person ();
    persons [0].setName("Josef");
    persons [0].setHeight(170);
    persons [0].setAge(30);

    persons [1] = new Person ();
    persons [1].setName("Marie");
    persons [1].setHeight(160);
    persons [1].setAge(35);

    persons [2] = new Person ();
    persons [2].setName("Karel");
    persons [2].setHeight(180);
    persons [2].setAge(40);

    for (int i=0; i<persons.length; i++){
        System.out.println("Jméno: " + persons[i].getName()+ ", věk: " + persons[i].getAge() + ", vyška: " + persons[i].getHeight());
    }

    //My main problem is here
    for (int i = 0; i<persons.length; i++){
        System.out.println(persons[i].selectionSort());
    }

}
}

共 (1) 个答案

  1. # 1 楼答案

    你的代码有几个问题,你必须在网上学习并明确你的概念。不过,我要解释一下:

    System.out.println(persons[i].selectionSort());
    

    现在,您已经创建了这个方法selectionSort(),并且可以在类型为Person的对象上使用它,但是它需要一个类型为int[]的参数,您没有提供这个参数

    这是一个逻辑错误,selectionSort的工作方式不是不能对数组的每个索引调用此方法。他的工作是立即对阵列进行排序。因此,您必须传递整个persons[]数组,其余的将由selectionSort()完成

    public int[] selectionSort (int[] age)
    

    您正在使用int age[],您不能这样做,因为您没有类型为int的数组,您拥有的是类型为Person的数组。类型为Person的每个对象都有一个属性age,您可以通过dot operator访问它

    工作代码:

    public class Person 
    {
        public static Person [] persons = new Person [3];  // so that every method can access this array
        private String name;
        private int height;
        private int age;  
    
        public void Person (String name, int height, int age){
            this.name = name;
            this.height = height;
            this.age = age;
        }
    
        public String getName (){
            return name;
        }
    
        public int getHeight (){
            return height;
        }
    
        public int getAge (){
            return age;
        }
    
        public void setName (String name) {
            this.name = name;
        }
    
        public void setHeight (int height) {
            this.height=height;
        }
    
        public void setAge (int age) {
            this.age=age;
        }
    
        public static void selectionSort(Person persons[])
        {
            int smallest;
    
            for(int i = 0; i < persons.length; i++)
            {
                smallest=i;
                for(int index = i+1; index<persons.length; index++)
                    if(persons[index].age<persons[smallest].age)
                        smallest=index;
    
                swap(i,smallest);   
            }
        }
    
        public static void swap(int frst, int scnd)
        {
            Person temporary = persons[frst];
            persons[frst] = persons[scnd];
            persons[scnd] = temporary;
        }
    
        public static void main(String[] args) 
        {
            persons [0] = new Person ();
            persons [0].setName("Josef");
            persons [0].setHeight(170);
            persons [0].setAge(35);
    
            persons [1] = new Person ();
            persons [1].setName("Marie");
            persons [1].setHeight(160);
            persons [1].setAge(31);
    
            persons [2] = new Person ();
            persons [2].setName("Karel");
            persons [2].setHeight(180);
            persons [2].setAge(40);
    
            for (int i=0; i<persons.length; i++){
                System.out.println("Jmeno: " + persons[i].getName()+ ", vek: " + persons[i].getAge() + ", vyska: " + persons[i].getHeight());
            }
    
            selectionSort(persons);
            for (int i = 0; i<persons.length; i++){
                System.out.println(persons[i].age);
            }
        }
    }
    

    注意:我在同一个类Person中合并了代码,但您可以始终将它们分为MainPerson。如果您分割代码,建议在Main中包含selectionSort()