Java bluej。从arrayList中获取下一个元素

问题描述 投票:0回答:1

我是使用bluej练习Java的初学者,并在每次使用getNext()方法时尝试打印下一个元素。我尝试了一些选项,但它们无效,现在我陷入了困境。这是我的代码:

public void getNextQuestion()
{
    int counter = 0;
    Iterator<Questions> it = this.questions.iterator();
    while(it.hasNext())
    {
        counter = counter + 1;
        Questions nextObject = it.next();

        System.out.println(counter+ ". " + nextObject.getDescription());


    }
}
java arraylist iterator bluej
1个回答
0
投票

我想您只想在调用getNextQuestion时打印一个问题。在这种情况下,您需要这样做:

public class MyClass {

    int counter = 0;

    public void getNextQuestion()
    {
        Questions nextObject = questions.get(counter);
        counter = counter + 1;

        System.out.println(counter+ ". " + nextObject.getDescription());
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.