SS帆 2017-09-02 15:50 采纳率: 100%
浏览 1209
已采纳

Java字符串排序,小白求助~

import java.io.*;
 import java.util.*;
 import java.util.Comparator;
 public class Main {
    @SuppressWarnings("rawtypes")
    public static void main(String[] args) throws Exception {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        ArrayList songList = new ArrayList(n);
        String str = cin.nextLine();
        while (str != null) {
            str = cin.nextLine();
            if (str.length() == 1 || str.length() == 0)
                continue;
            songInfo a = new songInfo(str);
            songList.add(a);
        }
        cin.close();
        Collections.sort(songList, new sort());
        System.out.println(songList);
    }
 }
 class songInfo {
    public String title;
    public String composer;
    public int runningTime;
    public songInfo(String str) {
        String[] song = str.split("&");
        this.title = song[0];
        this.composer = song[1];
        this.runningTime = Integer.parseInt(song[2], 10);
        ;
    }
    public String getTitle() {
        return title;
    }
    public String getComposer() {
        return composer;
    }
    public int getRunningTime() {
        return runningTime;
    }
    public String toString() {
        return this.title + this.composer + this.runningTime;
    }
  }
 class sort implements Comparator {
    @Override
    public int compare(Object o1, Object o2) {
        songInfo s1 = (songInfo) o1;
        songInfo s2 = (songInfo) o2;
        if (s1.getRunningTime() < s2.getRunningTime())
            return 1;
        return -1;
    }
  }

/*
输入样本:
3
&
Pink Frost&Phillipps, Martin&234933
Se quel guerrier io fossi&Puccini, Giacomo&297539
Non piu andrai&Mozart&234933
M'appari tutt'amor&Flotow, F&252905
输出:
Se quel guerrier io fossi&Puccini, Giacomo&297539
M'appari tutt'amor&Flotow, F&252905
Non piu andrai&Mozart&234933
*/

输入第一行为输出的数量,第二行为分隔符,第三行开始是字符串由&分隔为三部分。
根据数字部分排序,遇到相同的根据第一部分字母排序。

才自学java三个星期,写了几个小时就写出来上面那一片,现在没有头绪了。
能不能请帮忙改到有正确的输出?
直接加我微信shishunfan2 也行,改完发20红包感谢啊

  • 写回答

1条回答

  • 鼠晓 博客专家认证 2017-09-03 04:00
    关注

    不知道题主解决了没,,,,还有红包吗,(●'◡'●),,,代码奉上,,随意大赏

     import java.io.*;
    import java.util.*;
    import java.util.Comparator;
    
    public class demo {
        public static void main(String[] args) throws Exception {
            Scanner cin = new Scanner(System.in);
            int n = cin.nextInt();
            ArrayList<SongInfo> songList = new ArrayList<SongInfo>(n);
            cin.nextLine();//吃掉回车
            String split = cin.nextLine();
            String str;
            for (int i = 0; i < n; i++) {
                str = cin.nextLine();
                if (str.length() == 1 || str.length() == 0)
                    continue;
                SongInfo a = new SongInfo(str, split);
                songList.add(a);
    
            }
            cin.close();
            Collections.sort(songList);
            for (SongInfo song : songList) {
                System.out.println(song);
            }
        }
    }
    
    class SongInfo implements Comparable {
        public String title;
        public String composer;
        public int runningTime;
    
        public SongInfo(String str, String spilitStr) {
            String[] song = str.split(spilitStr);
            this.title = song[0];
            this.composer = song[1];
            this.runningTime = Integer.parseInt(song[2], 10);
        }
    
        public String getTitle() {
            return title;
        }
    
        public String getComposer() {
            return composer;
        }
    
        public int getRunningTime() {
            return runningTime;
        }
    
        public String toString() {
            return this.title + this.composer + this.runningTime;
        }
    
        /**
         * 比较两个字符串
         *
         * @param str1
         * @param str2
         * @return
         */
        private int compareStr(String str1, String str2) {
            if (str1.equals(str2)) return 0;
            char[] str1Chars = str1.toCharArray();
            char[] str2Chars = str2.toCharArray();
            int less = str1Chars.length > str2Chars.length ? str1Chars.length : str2Chars.length;//取最小
            for (int i = 0; i < less; i++) {
                if (str1Chars[i] == str2Chars[i]) continue;
                if (str1Chars[i] > str2Chars[i]) {
                    return 1;
                } else {
                    return -1;
                }
            }
            return 0;
        }
    
        @Override
        public int compareTo(Object o) {
            SongInfo s1 = this;
            SongInfo s2 = (SongInfo) o;
            if (s1.getRunningTime() > s2.getRunningTime()) return -1;
            if (s1.getRunningTime() < s2.getRunningTime()) return 1;
            return compareStr(s1.getTitle(), s2.getTitle());
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog