You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java按地点降序排序姓名的实现及代码补全请求

没问题,我来帮你补全这个Java程序的逻辑!先理清楚核心需求:从用户处收集多组姓名和地点信息,存储后按地点降序排序,最终只展示排序后的姓名。我会给你两种实现方案,一种是更易读、易维护的对象封装方式,另一种是严格贴合你要求的字符串数组存储方式,你可以根据实际场景选择。


方案一:使用自定义类封装数据(推荐)

这种方式用一个类来绑定姓名和地点,逻辑更清晰,后续扩展也更方便(比如要增加年龄字段):

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

// 封装姓名和地点的实体类
class PersonLocation {
    private String name;
    private String location;

    // 构造方法,初始化姓名和地点
    public PersonLocation(String name, String location) {
        this.name = name;
        this.location = location;
    }

    // 获取姓名的 getter 方法
    public String getName() {
        return name;
    }

    // 获取地点的 getter 方法
    public String getLocation() {
        return location;
    }
}

public class LocationSortApp {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 用动态数组存储用户输入的信息
        ArrayList<PersonLocation> personList = new ArrayList<>();

        // 引导用户输入
        System.out.println("请输入姓名和地点(格式:姓名 地点),输入「quit」结束输入:");
        while (true) {
            String input = scanner.nextLine().trim();
            // 输入quit时终止输入
            if (input.equalsIgnoreCase("quit")) {
                break;
            }
            // 拆分输入内容:按空格拆分,最多拆成2部分,兼容姓名带空格的情况(比如"John Doe London")
            String[] inputParts = input.split("\\s+", 2);
            if (inputParts.length == 2) {
                personList.add(new PersonLocation(inputParts[0], inputParts[1]));
            } else {
                System.out.println("输入格式错误,请重新输入:姓名 地点");
            }
        }

        // 按地点降序排序:用Collections.sort配合自定义Comparator
        Collections.sort(personList, new Comparator<PersonLocation>() {
            @Override
            public int compare(PersonLocation p1, PersonLocation p2) {
                // 降序排序:用p2的地点和p1比较,忽略大小写(比如"London"和"london"视为相同)
                return p2.getLocation().compareToIgnoreCase(p1.getLocation());
            }
        });

        // 输出排序后的姓名
        System.out.println("\n按地点降序排列后的姓名:");
        for (PersonLocation person : personList) {
            System.out.println(person.getName());
        }

        scanner.close();
    }
}

方案二:使用字符串数组存储数据

如果要求必须用字符串数组存储,我们可以把每组姓名和地点用分隔符拼接成一个字符串,排序时再拆分比较:

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class LocationSortWithStringArray {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 初始化一个足够大的字符串数组(这里假设最多存储100条数据)
        String[] personArray = new String[100];
        int dataCount = 0;

        // 引导用户输入
        System.out.println("请输入姓名和地点(格式:姓名 地点),输入「quit」结束输入:");
        while (true) {
            String input = scanner.nextLine().trim();
            if (input.equalsIgnoreCase("quit")) {
                break;
            }
            String[] inputParts = input.split("\\s+", 2);
            if (inputParts.length == 2) {
                // 用「|」作为分隔符拼接姓名和地点(避免和姓名/地点中的字符冲突)
                personArray[dataCount++] = inputParts[0] + "|" + inputParts[1];
            } else {
                System.out.println("输入格式错误,请重新输入:姓名 地点");
            }
        }

        // 截取数组中的有效数据(去掉未使用的空元素)
        String[] validDataArray = Arrays.copyOf(personArray, dataCount);

        // 按地点降序排序:自定义Comparator拆分字符串获取地点进行比较
        Arrays.sort(validDataArray, new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                // 拆分字符串获取地点
                String location1 = s1.split("\\|")[1];
                String location2 = s2.split("\\|")[1];
                // 降序排序,忽略大小写
                return location2.compareToIgnoreCase(location1);
            }
        });

        // 输出排序后的姓名
        System.out.println("\n按地点降序排列后的姓名:");
        for (String personData : validDataArray) {
            // 拆分字符串获取姓名并输出
            System.out.println(personData.split("\\|")[0]);
        }

        scanner.close();
    }
}

关键细节说明
  • 输入处理:用split("\\s+", 2)拆分输入,确保姓名包含空格的情况也能正确识别(比如"Jane Smith Paris"会被拆成姓名"Jane Smith",地点"Paris")。
  • 排序逻辑:使用compareToIgnoreCase实现不区分大小写的排序,更符合日常使用场景;降序排序通过交换比较对象的顺序实现(用后一个元素的地点和前一个比)。
  • 数据存储:对象封装方式更适合实际项目,便于维护和扩展;字符串数组方式严格贴合题目要求,但要注意选择不会出现在内容中的分隔符(比如|),避免拆分错误。

内容的提问来源于stack exchange,提问作者raj535

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:33:04