Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用带有LinkedList的compareTo方法按字母顺序添加对象_Java_Linked List_Compareto - Fatal编程技术网

Java 使用带有LinkedList的compareTo方法按字母顺序添加对象

Java 使用带有LinkedList的compareTo方法按字母顺序添加对象,java,linked-list,compareto,Java,Linked List,Compareto,我已经创建了自己的LinkedList,其中包含一个歌曲对象(标题、艺术家等)。我试图添加一个功能,用户输入歌曲详细信息,并按艺术家的字母顺序将歌曲添加到列表中。我的问题是,我不知道如何设置if-else语句以按艺术家的字母顺序将其添加到列表中,提前感谢 歌曲类中的compareTo() //Should return negative if in order, positive if out of order, 0 if equal public int compareTo(Song s) {

我已经创建了自己的LinkedList,其中包含一个歌曲对象(标题、艺术家等)。我试图添加一个功能,用户输入歌曲详细信息,并按艺术家的字母顺序将歌曲添加到列表中。我的问题是,我不知道如何设置if-else语句以按艺术家的字母顺序将其添加到列表中,提前感谢

歌曲类中的compareTo()

//Should return negative if in order, positive if out of order, 0 if equal
public int compareTo(Song s) {
        return title.compareTo(s.getTitle()); //I think this is correct?
}
public static void addSong(LinkedList list){
    Scanner input = new Scanner(System.in);

    System.out.println("Enter song title: ");
    String title = input.nextLine();
    System.out.println("Enter song artist: ");
    String artist = input.nextLine();
    System.out.println("Enter song album: ");
    String album = input.nextLine();
    System.out.println("Enter song length: ");
    String length = input.nextLine();

    Song songInfo = new Song(title, artist, album, length);
    for (int i = 0; i < list.size(); i ++){
        if ( //compareTo should be used here < 0 ){
        }else if( //compareTo should be used here == 0)

    }
public class Node {
private Song song;
private Node next;
public Node( Song s ){
    song = s;
    next = null;
}
public Node( Song s, Node n ){
    song = s;
    next = n;
}
public Node getNext(){
    return next;
}
public void setNext(Node n){
    next = n;
}
public Song getValue(){
    return song;
}
public void setValue( Song s ){
    song = s;
}
主类

//Should return negative if in order, positive if out of order, 0 if equal
public int compareTo(Song s) {
        return title.compareTo(s.getTitle()); //I think this is correct?
}
public static void addSong(LinkedList list){
    Scanner input = new Scanner(System.in);

    System.out.println("Enter song title: ");
    String title = input.nextLine();
    System.out.println("Enter song artist: ");
    String artist = input.nextLine();
    System.out.println("Enter song album: ");
    String album = input.nextLine();
    System.out.println("Enter song length: ");
    String length = input.nextLine();

    Song songInfo = new Song(title, artist, album, length);
    for (int i = 0; i < list.size(); i ++){
        if ( //compareTo should be used here < 0 ){
        }else if( //compareTo should be used here == 0)

    }
public class Node {
private Song song;
private Node next;
public Node( Song s ){
    song = s;
    next = null;
}
public Node( Song s, Node n ){
    song = s;
    next = n;
}
public Node getNext(){
    return next;
}
public void setNext(Node n){
    next = n;
}
public Song getValue(){
    return song;
}
public void setValue( Song s ){
    song = s;
}
节点类

//Should return negative if in order, positive if out of order, 0 if equal
public int compareTo(Song s) {
        return title.compareTo(s.getTitle()); //I think this is correct?
}
public static void addSong(LinkedList list){
    Scanner input = new Scanner(System.in);

    System.out.println("Enter song title: ");
    String title = input.nextLine();
    System.out.println("Enter song artist: ");
    String artist = input.nextLine();
    System.out.println("Enter song album: ");
    String album = input.nextLine();
    System.out.println("Enter song length: ");
    String length = input.nextLine();

    Song songInfo = new Song(title, artist, album, length);
    for (int i = 0; i < list.size(); i ++){
        if ( //compareTo should be used here < 0 ){
        }else if( //compareTo should be used here == 0)

    }
public class Node {
private Song song;
private Node next;
public Node( Song s ){
    song = s;
    next = null;
}
public Node( Song s, Node n ){
    song = s;
    next = n;
}
public Node getNext(){
    return next;
}
public void setNext(Node n){
    next = n;
}
public Song getValue(){
    return song;
}
public void setValue( Song s ){
    song = s;
}

只需浏览列表,直到您发现第一个元素比您的元素大。然后在该元素之前添加元素。因此,您需要<代码>实现可重复的< /代码>,然后您需要实现<代码> Iterator <代码>,它支持<代码>添加< /代码>,在您的<代码>列表中。修改您的代码> Addio< /Cult>逻辑,以找到适当的索引,在这里插入“代码>节点< /代码>,而不是总是最后添加。好吧,我还是有点困惑。我不确定if语句是看起来像这样,还是完全关闭了if(artist.compareTo(list.get(I).getValue().getArtist())<0)**您永远不应该从
链接列表中
get(I)
;提示:在
List`上循环,并为每个元素调用get(i)——时间复杂度是多少?因此,您需要
实现Iterable
,并查看它将把您带到哪里。