LinkedList内部的LinkedList迭代在Java中

huangapple go评论87阅读模式
英文:

LinkedList inside a LinkedList iteration in java

问题

我尝试迭代一个嵌套的链表,但我不确定如何继续进行。通常,我习惯使用传递的参数来确定要迭代的内容,但当我迭代一个嵌套的链表并计划一直迭代到找到与传递的虚拟对象匹配的记录时,我不太确定该怎么做。

以下是我尝试做的示例代码:

private static boolean addSongFromAlbumToAlbum(LinkedList<Album> albums1, LinkedList<Song> targetAlbum,
                                                 String title){
    // 为了与标题参数进行比较,创建一个虚拟歌曲,其持续时间为任意值。
    Song dummySong = new Song(title, 000);

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

    // 检查歌曲是否存在于专辑的链表中
    while(album1ListIterator.hasNext()){
        Album currentAlbum = album1ListIterator.next();
        LinkedList<Song> nestedAlbum = currentAlbum.getSongs(); // 获取嵌套的歌曲链表

        ListIterator<Song> nestedAlbumInAlbum = nestedAlbum.listIterator();

        while(nestedAlbumInAlbum.hasNext()){
            // 检查当前迭代是否有与标题参数相同的歌曲
            Song comparisonSongToAdd = nestedAlbumInAlbum.next();
            int comparisonValue = comparisonSongToAdd.getTitle().compareTo(title);
            
            if(comparisonValue == 0){
                // 检查找到的对象是否已存在于目标专辑中
                boolean songExistsInTargetAlbum = false;
                ListIterator<Song> targetAlbumListIterator = targetAlbum.listIterator();
                
                while (targetAlbumListIterator.hasNext()){
                    SongComparator comparator = new SongComparator(); // 创建新的比较器对象进行比较
                    int comparatorValue = comparator.compare(comparisonSongToAdd, targetAlbumListIterator.next());
                    
                    if (comparatorValue == 0) {
                        System.out.println(comparisonSongToAdd + " 已经存在于专辑中,请选择其他歌曲。");
                        songExistsInTargetAlbum = true;
                        break;
                    }
                }
                
                if (!songExistsInTargetAlbum) {
                    targetAlbum.add(comparisonSongToAdd);
                }
            }
        }
    }
    return true;
}

在这个示例中,我们首先迭代外部专辑链表,然后在每个专辑中迭代嵌套的歌曲链表。这样,您可以检查每个专辑中是否存在与标题参数匹配的歌曲,并将其添加到目标专辑中,如果它不存在于目标专辑中的话。

英文:

Im trying to iterate a Linked List inside of a linked list but I'm not sure how to proceed with it. I'm used to using a passed parameter for what will be iterated but when I'm iterating a linked list within a linked list and plan to iterate until I hit a record that matches a passed dummy object.

Here is an example of what I’m trying to do

private static boolean addSongFromAlbumToAlbum(LinkedList&lt;Album&gt; albums1, LinkedList&lt;Song&gt; targetAlbum,
String title){
//creating a dummy song for comparison of title parameter with arbitrary time duration.
Song dummySong = new Song(title, 000);
ListIterator&lt;Album&gt; album1ListIterator = albums1.listIterator();
ListIterator&lt;Song&gt; targetAlbumListIterator = targetAlbum.listIterator();
//nested album iterator
ListIterator&lt;Song&gt; nestedAlbumInAlbum = nestedAlbum.listIterator();
//checking whether the song with the &quot;title&quot; 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 + &quot; already exists in the Album. please choose\n a different song.&quot;);
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

///Here is the SongComparator class

public class SongComparator implements Comparator&lt;Song&gt; {
public int compare(Song song1, Song song2){
if(song1.getTitle() == song2.getTitle() &amp;&amp; song1.getDurationSeconds() == song2.getDurationSeconds()){
return 0;
}else{
return -1;
}
}
}

How am I supposed to iterate the LinkedList within the LinkedList of albums without a parameter? And if it requires a parameter how I’m I supposed to determine what to use for the parameter considering it will be changing with each iteration of the outer while loop.

答案1

得分: 1

你可以使用Java 8的流(Streams),而不是创建迭代器,来查找专辑1和专辑2中的内容:

albums1.forEach(album1Element -> {
    // 仅保留返回true的过滤结果。
    List<Song> listOfSongsToAdd = targetAlbum.filter(song -> song.compareTo(title)).collect(Collectors.toList);
    listOfSongsToAdd.forEach(songToAdd -> {
        // ...
    });
});
英文:

you can use java 8 streams,
instead of creating iterators,

in order to find whats in album 1 and album 2 use:

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

答案2

得分: 1

在下面的代码行中,请注意,next() 返回一个 Song 对象,而 title 是一个 String 对象。这两者不能直接进行比较。您可能需要调用 nestedAlbumInAlbum.next() 并将对象存储在一个 Song 引用变量中,然后比较 song.getTitle()title

int comparisonValue = nestedAlbumInAlbum.next().compareTo(title);

此外,您多次调用了 nestedAlbumInAlbum.next()。一次是在上面,另一次是在 if 条件内部。您应该限制仅在迭代中调用一次 next() 方法。每次调用 nestedAlbumInAlbum.next() 时,它都会返回下一个 Song 对象。

英文:

Check below line of code. Here, next() returns a Song object and title is a String object. These both cannot be compared. You may have to call nestedAlbumInAlbum.next() and store the object in a Song reference variable and compare song.getTitle() with title.

int comparisonValue = nestedAlbumInAlbum.next().compareTo(title);

Also, you are calling nestedAlbumInAlbum.next() multiple times. 1 instance is above, another instance is within the if condition. You should restrict calling next() method only once for iteration. Each time when you call nestedAlbumInAlbum.next(), it returns the next Song object.

huangapple
  • 本文由 发表于 2020年7月30日 01:55:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63159698.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定