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());
        }
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮