ListOperator 在方法中的用法

ListOperator usage in methods

我不知道如何共享我的所有代码或 类 来解释问题所在。但我会尝试这个小部分。我有视频类型的链表。我想播放下一个视频。用这个方法。在 switch-case 循环中。

    public static void playTheList (LinkedList<Video> video) {

    Scanner sc = new Scanner(System.in);
    System.out.println("Choice: ");
    int choice= sc.nextInt();
    boolean out = false;

    while (!out) {
    switch(choice){
    case 1:
    if(iterator.hasNext()){
    Video firstVideo=iterator.next();
    System.out.println("****");
    System.out.println(" Playing video Name :"+firstVideo.getVideoName()+" Video length: "+firsVideo.getVideoLenghth());

    }
    else{
    System.out.println("End of List");
    }
    break;

    case 2:
    playNextVid(videoList);
    break;
    
    case 3:
    out=true;
    break;
    }
    }
    }

每次我按“1”时,我都想看到 LinkedList 的下一个元素,直到列表结束。问题是当我在 switch-case 循环中进行迭代时,它起作用了。但是当我尝试创建新方法并尝试调用该方法时。这些方法在该范围内工作并完成,因此不记得迭代在哪里。所以它不起作用。案例 1 有效,案例 2 无效。我怎样才能解决这个问题?我希望你能明白哪里出了问题。对不起英语。

    public static void playNextVid(LinkedList<Video> video){

    ListIterator<Videos> iterator=video.listIterator();

    if(iterator.hasNext()){
    Video firstVideo=iterator.next();
    System.out.println("****");
    System.out.println(" Playing video Name :"+firstVideo.getVideoName()+" Video length: "+firstVideo.getVideoLenghth());

    }
    else{
    System.out.println("End of List");

    }
    }

I don't know how can I share all my codes or classes to explain what's wrong.

没有必要。您应该 post 一个 mre 如下所示:

import java.util.*;

public class Main {

    public static void main(String[] args) throws Exception{
        List<String> video = Arrays.asList("video S01E01", "video S01E02","video S01E03", "video S01E04");
        Iterator<String> iterator = video.iterator();
        for (int i = 0; i <= video.size(); i++) {
            handleChoice(iterator, 2);
        }
    }

    private static void handleChoice(Iterator<String> iterator, int choice){
         switch(choice){
            case 1:
                if(iterator.hasNext()){
                    System.out.println(" Playing video Name :"+iterator.next());
                }
                else{
                    System.out.println("End of List");
                }
                break;

            case 2:
                playNextVid(iterator);
                break;
         }
    }

    private static void playNextVid(Iterator<String> iterator) {

           if(iterator.hasNext()){
                System.out.println(" Playing video Name :"+iterator.next());
            }
            else{
                System.out.println("End of List");
            }
    }
}