java冒泡排序什么毛病


package com.wk.tt;

public class t {
    public static void main(String[] args) {
        person [] persons=new  person  [3];
       persons[0] = new person("刷个",18,"演员");
      persons[1] = new person("李白", 24, "诗人");
      persons [2]=new person("杜甫",30,"杜甫");
      for(int i=0;i-1;i++){
          for(int j=0;j< persons.length-i-1;j++){
              if(persons[j].getAge()>persons[j+1].getAge()){
                  int tmp=0;
                  tmp=persons[j].getAge();
                  persons[j].getAge()=persons[j+1].getAge();
                  persons[j+1].getAge()=tmp;
                  
                  
              }
          }
      }
        
        
        

    }
}

  • 你这 只是交换了局部变量的 age 吧,也没有将对应数据设置到person类里,另外 其他的数据也还是原来
  • 参考如下:
import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
        person[] persons= new  person  [3];
        persons[0] = new person("刷个",25,"演员");
        persons[1] = new person("李白", 24, "诗人");
        persons [2]=new person("杜甫",30,"诗人");
        for(int i=0;i<persons.length-1;i++){
            for(int j=0;j< persons.length-i-1;j++){
                if(persons[j].getAge()>persons[j+1].getAge()){
                    person tmp = persons[j];
                    persons[j] = persons[j+1];
                    persons[j+1] = tmp;
                }
            }
        }
        System.out.println(Arrays.toString(persons));
    }

    private static class person {
        private String name;
        private int age;
        private String job;

        public person(String name, int age, String job) {
            this.name = name;
            this.age = age;
            this.job = job;
        }

        public String getName() {
            return name;
        }

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

        public int getAge() {
            return age;
        }

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

        public String getJob() {
            return job;
        }

        public void setJob(String job) {
            this.job = job;
        }

        @Override
        public String toString() {
            return "person{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    ", job='" + job + '\'' +
                    '}';
        }
    }
}
 
 

对于person对象的冒泡排序存在问题。
1、冒泡排序是需要交换对象本身而不是属性值的,但是代码中使用getAge()获取属性值后再进行交换,这是错误的。
2、交换两个person对象需要交换所有属性值,而不仅仅是年龄属性,但是代码中只交换了年龄属性,这是错误的。
正确的做法应该是重写person类的compareTo方法,通过比较年龄属性来实现person对象的比较,然后使用Arrays.sort方法对persons数组进行排序。

你可以这样试试:有用的话给我点个关注就行!

package com.wk.tt;

public class t {
    public static void main(String[] args) {
        person[] persons = new person[3];
        persons[0] = new person("刷个", 18, "演员");
        persons[1] = new person("李白", 24, "诗人");
        persons[2] = new person("杜甫", 30, "杜甫");
        for (int i = 0; i < persons.length - 1; i++) {
            for (int j = 0; j < persons.length - i - 1; j++) {
                if (persons[j].getAge() > persons[j + 1].getAge()) {
                    int tmp = persons[j].getAge();
                    persons[j].setAge(persons[j + 1].getAge());
                    persons[j + 1].setAge(tmp);
                }
            }
        }
    }
}
主要是在交换两个 person 对象时直接调用对象的 setAge 方法修改年龄

你应该交换对象,而不是age
类似这样

package com.wk.tt;
 
public class t {
    public static void main(String[] args) {
        person [] persons=new  person  [3];
       persons[0] = new person("刷个",18,"演员");
      persons[1] = new person("李白", 24, "诗人");
      persons [2]=new person("杜甫",30,"杜甫");
      for(int i=0;i<persons.length-1;i++){
          for(int j=0;j< persons.length-i-1;j++){
              if(persons[j].getAge()>persons[j+1].getAge()){
                  person tmp=null;
                  tmp=persons[j];
                  persons[j]=persons[j+1];
                  persons[j+1]=tmp;
                  
                  
              }
          }
      }
        
        
        
 
    }
}