java 中 LinkedList 迭代中的 LinkedList

LinkedList inside a LinkedList iteration in java

我正在尝试在链表中迭代链表,但我不确定如何进行。我习惯于将传递的参数用于将要迭代的内容,但是当我在链表中​​迭代链表并计划迭代直到找到与传递的虚拟对象匹配的记录时。

这是我正在尝试做的一个例子

private static boolean addSongFromAlbumToAlbum(LinkedList<Album> albums1, LinkedList<Song> targetAlbum,
                                             String title){
//creating a dummy song for comparison of title parameter with arbitrary time duration.
        Song dummySong = new Song(title, 000);

        ListIterator<Album> album1ListIterator = albums1.listIterator();
        ListIterator<Song> targetAlbumListIterator = targetAlbum.listIterator();

        //nested album iterator
        ListIterator<Song> nestedAlbumInAlbum = nestedAlbum.listIterator();

        //checking whether the song with the "title" parameter entered exists in the LinkedList
        //of albums
        while(album1ListIterator.hasNext()){

            while(nestedAlbumInAlbum.hasNext()){

                //checking if current iteration has an object with same value for title as title parameter.

                Song comparisonSongToAdd = nestedAlbumInAlbum.next();
                int comparisonValue = comparisonSongToAdd.getTitle().compareTo(title);
                if(comparisonValue ==0){

                    //check whether the found object already exists in the album
                    while (targetAlbumListIterator.hasNext()){
                        SongComparator comparator = new SongComparator(); //create new comparator object to compare

                        int comparatorValue = comparator.compare(comparisonSongToAdd, targetAlbumListIterator.next());
                        if (comparatorValue == 0) {
                            System.out.println(comparisonSongToAdd + " already exists in the Album. please choose\n a different song.");
                            return false;

                        }//end if comparator

                    }//end target album while
                    targetAlbumListIterator.add(comparisonSongToAdd);

                }//end if song title found

            }//end nested album while
        }//end albums while iterator
        return true;

    }//end addSongFromAlbum method

///这里是SongComparatorclass

public class SongComparator implements Comparator<Song> {

    public int compare(Song song1, Song song2){
        if(song1.getTitle() == song2.getTitle() && song1.getDurationSeconds() == song2.getDurationSeconds()){
            return 0;
        }else{
            return -1;
        }
    }

}

我应该如何在没有参数的情况下在相册的 LinkedList 中迭代 LinkedList?如果它需要一个参数,考虑到它会随着外部 while 循环的每次迭代而改变,我应该如何确定该参数的用途。

您可以使用 java 8 个流, 而不是创建迭代器,

要查找相册 1 和相册 2 中的内容,请使用:

    albums1.forEach(album1Element -> {
        //keeps only what returns true in the filter.
        List<Song> listOfSongsToAdd = targetAlbum.filter(song -> song.compareTo(title)).collect(Collectors.toList);
        listOfSongsToAdd.forEach(songToAdd -> {
           ...

        });

    });

检查下面的代码行。这里,next() returns 是一个 Song 对象,而 title 是一个 String 对象。这两者不能相提并论。您可能必须调用 nestedAlbumInAlbum.next() 并将对象存储在 Song 引用变量中,然后将 song.getTitle()title.

进行比较
int comparisonValue = nestedAlbumInAlbum.next().compareTo(title);

此外,您多次呼叫 nestedAlbumInAlbum.next()。 1 个实例在上面,另一个实例在 if 条件内。您应该限制只调用一次 next() 方法进行迭代。每次调用 nestedAlbumInAlbum.next() 时,它都会 returns 下一个 Song 对象。