帮我理解title.compareTo(a.title())的幕后故事; codeInside

时间:2016-12-18 12:41:36

标签: java string sorting collections compareto

//Sorting userDefined object from ArrayList<>...    

import java.io.*;
import java.util.*;

class Song implements Comparable<Song>{
    String title;
    String movie;
    String rating;

    public int compareTo(Song s){
        //System.out.println(getTitle()+"   "+s.getTitle());
        /*The upper comment is for testing purpose, 
         *because i wanted to see whats the value getTitle() compares..
         *but i couldn't understand.
         */
        return getTitle().compareTo(s.getTitle());
    }

    public Song(String t, String m, String r){
    //R.I.P. Naming Convention.
        title = t;
        movie = m;
        rating = r;
    }

    public String getTitle(){
        return title;
    }

    public String toString(){
        return title;
    }
}

class ArrayListDemo{
    ArrayList<Song> songsList = new ArrayList<Song>();
    public static void main(String[] args){
        new ArrayListDemo();
    }

    public ArrayListDemo(){
        getSongs();
        System.out.println(songsList); 
        Collections.sort(songsList);   
        System.out.println(songsList);
    }

    public void getSongs(){
        try{
            File file = new File("SongsList.txt");
            //check below for SongsList.txt 
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line = null;
            while((line = reader.readLine()) != null){
                addSong(line);
            }
        } catch(IOException e){
            e.printStackTrace();
        }
    }

    public void addSong(String lineToParse){
        String[] token = lineToParse.split("/");
        Song nextSong = new Song(token[0], token[1], token[2]); 
        songsList.add(nextSong);
    }
}

SongsList.txt
Soch Na Sake / Airlift / 9.1
Jeena / Badlapur / 8.7
Tere Sang Yaara / Rustom / 8.8
Aayat Ki Tarah / BaajiravMastaani / 7.9
Ikk Kudi / UdtaPunjab / 7.5
Tay Hai / Rustom / 7.8

Output:-
Before Sorting...
[Soch Na Sake , Jeena , Tere Sang Yaara , Aayat Ki Tarah , Ikk Kudi , Tay Hai ]

After Sorting...
[Aayat Ki Tarah , Ikk Kudi , Jeena , Soch Na Sake , Tay Hai , Tere Sang Yaara ]

警告: - 初级英语未来!!!

所以这是我的程序的工作...参考:-HeadFirstJava 2nd,JukeBox3 Page no.- 550

所以,遇到问题..

我理解他们中的大部分......但这是我的思绪在滚动的地方.. o.O

public int compareTo(Song s){
    return getTitle().compareTo(s.getTitle());
}

getTitle()&amp; s.getTitle()

从getTitle()获取值并比较它... 好吧,我知道compareTo()比较String但我也知道-1,0,1(&lt;,=,&gt;)规则, 破解我的是getTitle()获取值的地方。 还有一件事......书上写着

Collections.sort(songsList);

当调用此方法时,sort方法将一个元素从songsList发送到compareTo()方法。表示s.getTitle()=由sort()发送的元素的标题。对吗?

但是从第一个getTitle()获取值的位置......返回之后和.compareTo()之前的值。

请在这里帮助我,检查文档,其他答案,最近两天我能做的一切......

简单来说,我想知道getTitle()从何处和哪些值进行比较。

1 个答案:

答案 0 :(得分:0)

public int compareTo(Song s)用于比较2个Song个对象。其中一个是调用方法的那个,另一个是作为参数传递的。

例如:

Song s1 = ...
Song s2 = ...
int s1.compareTo(s2);

在此示例中,getTitle()正文中的public int compareTo(Song s)将返回s1的标题,而s.getTitle()将返回s2的标题。< / p>

Collections.sort(songsList);将始终使用compareTo来比较2个Song个对象 - 其中一个将调用方法,另一个将作为参数传递。