繁体   English   中英

遍历对象的自定义LinkedList类

[英]Iterate Through Custom LinkedList Class of Objects

我创建了自己的LinkedList类,并创建了一个LinkedList ,其中将包含对象Song(包含标题,艺术家,专辑,长度)。 我遇到的错误是,当尝试遍历列表时,我得到了“只能遍历java.lang.Iterable数组”。 我认为我的问题是我正在遍历类实例,因此在链接列表类中缺少某些东西来能够进行这种类型的迭代。 尽管不确定,但我不确定我需要添加什么。

这是我尝试迭代的地方:

System.out.print("Enter song title: ");
String searchTitle = input.nextLine();
for ( Song i : list ){
    if ( i.getTitle() == searchTitle ){
        System.out.println(i);
        found = true;
    }
}
if ( found != true ){
    System.out.println("Song does not exist.");
}

我的LinkedList类

public class LinkedList {

private Node first;

private Node last;

public LinkedList(){
    first = null;
    last = null;
}

public boolean isEmpty(){
    return first == null;
}

public int size(){
    int count = 0;
    Node p = first;
    while( p != null ){
        count++;
        p = p.getNext();
    }
    return count;
}

public Node get( int i ){
    Node prev = first;
    for(int j=1; j<=i; j++){
        prev = prev.getNext();
}
    return prev;
}

public String toString(){
    String str = "";
    Node n = first;
    while( n != null ){
        str = str + n.getValue() + " ";
         n = n.getNext();
    }
    return str;
}

public void add( Song c ){
    if( isEmpty() ) {
        first = new Node(c);
        last = first;
    }else{
        Node n = new Node(c);
        last.setNext(n);
        last = n;
    }
}

歌班

public class Song {

    private String title;

    private String artist;

    private String album;

    private String length;

    private static int songCounter = 0;

    public Song(String title, String artist, String album, String length){
        this.title = title;
        this.artist = artist;
        this.album = album;
        this.length = length;
        songCounter++;
    }

    public String getTitle(){
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getArtist(){
        return artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }

    public String getAlbum(){
        return album;
    }

    public void setAlbum(String album){
        this.album = album;
    }

    public String getLength(){
        return length;
    }

    public void setLength(String length){
        this.length = length;
    }

    public static int getSongCounter(){
        return songCounter;
    }

    public int compareArtist(Song o){
        return artist.compareTo(o.artist);
    }

    public int compareTitle(Song o){
        return title.compareTo(o.title);
    }
    @Override
    public String toString(){
        return title +","+artist+","+album+","+length;
    }

该错误消息非常明确:

只能迭代java.lang.Iterable数组。

这意味着您的类必须实现Iterable接口。

对于您的情况,必须实现此功能的类必须是LinkedList

public class LinkedList implements Iterable<Song> {
    //implement methods in Iterable interface
}

您也可以升级 LinkedList实现以处理通用元素,而不仅仅是Song对象引用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM