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

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条回答 默认 最新

  • Small_Mouse0
    鼠小 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());
        }
    }
    
    
    点赞 评论

相关推荐