Java实现对象数组的按某个元素排序

Java实现对象数组的按某个元素排序,第1张

Java实现对象数组的按某个元素排序

如题,本文为笔者的笔记 希望能帮到你

package com.mxy.homework;
import java.util.Scanner;


public class CustomerTest02 {
    public static void main(String[] args) {
        CustomerHandle c1 = new CustomerHandle();

        do {
            Customer02 cust02 = new Customer02();

            System.out.println("添加客户的姓名:");
            cust02.name = new Scanner(System.in).next();
            System.out.println("添加客户的年龄:");
            cust02.age = new Scanner(System.in).nextInt();
            System.out.println("添加客户的积分:");
            cust02.integral = new Scanner(System.in).nextInt();

            c1.add(cust02);

            System.out.println("是否继续再添加一个客户信息(y/n)");
        } while ("Y".equalsIgnoreCase(new Scanner(System.in).next()));
        c1.showAll();

        System.out.println("按积分进行升序排列为:");
        c1.ascSort();
        System.out.println("按积分进行降序排列为:");
        c1.descSort();

    }

}

class CustomerHandle {
    Customer02[] cust = new Customer02[1000];

    public void add(Customer02 customer02s) {
        for (int i = 0; i < cust.length; i++) {
            if (cust[i] == null) {
                cust[i] = customer02s;
                break;
            }
        }
    }

    public void showAll() {
        System.out.println("名字t" + "t年龄t" + "t积分");
        System.out.println("-----------------------------");
        for (Customer02 abc : cust) {
            if (abc != null) {
                abc.getShow();
            }
        }
        System.out.println("-----------------------------");
    }

    public void ascSort() {
            for (int i = 0; i < cust.length; i++) {
                for (int j = 0; j < cust.length - 1 - i; j++) {
                    if (cust[j+1] != null) {
                    if (cust[j].integral > cust[j + 1].integral) {
                        Customer02 temp = cust[j];
                        cust[j] = cust[j + 1];
                        cust[j + 1] = temp;
                    }
                }
            }
        }
        System.out.println("名字t" + "t年龄t" + "t积分");
        System.out.println("-----------------------------");
        for (Customer02 abc : cust) {
            if (abc != null) {
                abc.getShow();
            }
        }
        System.out.println("-----------------------------");
    }

    public void descSort() {
        for (int i = 0; i < cust.length; i++) {
            for (int j = 0; j < cust.length - 1 - i; j++) {
                if (cust[j+1] != null) {
                    if (cust[j].integral < cust[j + 1].integral) {
                        Customer02 temp = cust[j];
                        cust[j] = cust[j + 1];
                        cust[j + 1] = temp;
                    }
                }
            }
        }
        System.out.println("名字t" + "t年龄t" + "t积分");
        System.out.println("-----------------------------");
        for (Customer02 abc : cust) {
            if (abc != null) {
                abc.getShow();
            }
        }
        System.out.println("-----------------------------");
    }

}

------------------------------------------------------分割线------------------------------------------------------

package com.mxy.homework;


public class Customer02 {
    String name;
    int age;
    int integral;

    public void getShow(){
        System.out.println(name +"t"+age+"t"+"t"+integral);
    }



}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4996555.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存