zoukankan      html  css  js  c++  java
  • 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]);
    }
    }
    }

    时间,请带我像一条小溪流般,安静地流淌,汇入爱的海洋。
  • 相关阅读:
    Redis分布式锁实现
    mysql索引命中规则
    spring注解原理
    img 标签访问图片返回403forbidden
    根据注解修改属性的值
    通过反射改变对象的属性
    利用反射获取类或者方法或者字段上的注解的值
    什么时候出现死锁,如何解决?mysql 引擎? 多个like or 查询sql如何优化?什么是常量池?for条件执行顺序
    jvm 基础
    为什么要用jvm .
  • 原文地址:https://www.cnblogs.com/1246447850qqcom/p/3977176.html
Copyright © 2011-2022 走看看