• Java冒泡排序,Java对象冒泡排序


    今天呆公司特别无聊,百度了一下Java机试题,看到一个冒泡排序。

    粘上我全部的代码:

    实体类:

    package accp.com.internet;
    /**
    * 人物类
    * @author xuxiaohua
    *
    */
    public class Psonse {

    private String name;
    private double age;
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public double getAge() {
    return age;
    }
    public void setAge(double age) {
    this.age = age;
    }

    }

    测试类:

    package accp.com.internet;
    import java.util.ArrayList;
    import java.util.List;
    /**
    * 测试类
    * @author xuxiaohua
    *
    */
    public class Test {
    public static void main(String args[]){
    Psonse p1=new Psonse();
    p1.setAge(new Integer(20));
    p1.setName("zhangsan");

    Psonse p2=new Psonse();
    p2.setAge(new Integer(15));
    p2.setName("lisisi");

    Psonse p3=new Psonse();
    p3.setAge(new Integer(50));
    p3.setName("liuxiaowei");

    List<Psonse> list=new ArrayList<Psonse>();
    list.add(p1);
    list.add(p2);
    list.add(p3);

    int[] arry=new int[list.size()];//申明一个数组
    for(int a=0;a<list.size();a++){
    Psonse age=list.get(a);
    arry[a]=(int) age.getAge();
    }

    /**

    *从小到大

    */
    int temp;
    for(int i=0;i<arry.length-1;i++){
    for(int j=0;j<arry.length-1-i;j++){
    if(arry[j]>arry[j+1]){
    temp=arry[j];
    arry[j]=arry[j+1];
    arry[j+1]=temp;
    }
    }
    }

    /**
    * 从大到小
    */
    // int tep;
    // for(int a=0;a<arry.length;a++){
    // for(int b=0;b<arry.length-1;b++){
    // tep=arry[b];
    // if(arry[b]<arry[b+1]){
    // arry[b]=arry[b+1];
    // arry[b+1]=tep;
    // }
    // }
    // }


    System.out.println("排序后的年龄从小到大:");
    for(int k=0;k<list.size();k++){
    System.out.println(arry[k]);
    }
    }
    }

    时间,请带我像一条小溪流般,安静地流淌,汇入爱的海洋。
  • 相关阅读:
    C++顺序容器知识总结
    C++标准库vector类型的使用和操作总结
    C++迭代器的使用和操作总结
    快速入门正则表达式
    深入浅出 Create React App
    JavaScript 中有关数组对象的方法
    JavaScript 中有关时间对象的方法
    Web前端小白入门指迷
    Mac OSX 下用 Homebrew 安装 MongoDB 并配置到 WebStorm 中
    面向对象三大特性五大原则 + 低耦合高内聚
  • 原文地址:https://www.cnblogs.com/1246447850qqcom/p/3977176.html
Copyright © 2020-2023  润新知