定义一个Person类{name,age,job},初始化Person对象数组,有三个person对象,并按照age从小到大进行排序,使用冒泡排序

**

定义一个Person类{name,age,job},初始化Person对象数组,有三个person对象,并按照age从小到大进行排序,使用冒泡排序


public class DemoA {
    public static void main(String[] args) {
        Person person[] = new Person[3];
        person[0] = new Person("张三", 19, "经理");
        person[1] = new Person("李四", 18, "董事长");
        person[2] = new Person("马六", 34, "职工");
        //输出当前对象数组
        for (int i = 0; i < person.length; i++) {
            System.out.println(person[i]);
        }
        //使用冒泡排序
        Person temp = null;// 临时变量,用于交换
        for (int i = 0; i < person.length - 1; i++) {//外层循环
            for (int j = 0; j < person.length - 1 - i; j++) {//内层循环
                //并按照从小到大进行排序,如果前面的人的age>后面人的年龄,就交换
                if (person[i].getAge()>person[i + 1].getAge()) {
                    temp = person[i];
                    person[i] = person[i + 1];
                    person[i + 1] = temp;
                }
            }
        }System.out.println("排序后的效果");
        for (int i = 0; i < person.length; i++) {
            System.out.println(person[i]);//默认对象的toString
        }


    }
}

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 + '\'' +
                '}';
    }
}