为什么ListIterator不能在我的Array List字段变量的构造函数中初始化?

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

此纸牌游戏创建了52张纸牌,并将它们随机排列在arraylist中。我试图通过在构造函数中添加迭代器来遍历列表,因为我想通过外部类访问listIterator功能,因此我尝试使用“卡”数组列表中的迭代器函数在此Cardlist类中创建一些方法,但我需要先初始化一个迭代器。我不明白为什么这行不通:

public class CardList {
private ArrayList<Card> cards;
private ListIterator<Card> listIterator;

public CardList() {
    this.cards = new ArrayList<>();
    createCardList();
    shuffleCardList();
    ListIterator<Card> listIterator = this.cards.listIterator();
    //added so card deck automatically made, shuffled, then an iterator is introduced
}
java arraylist iterator
1个回答
0
投票

构造函数用于初始化在类中声明的对象。在构造函数内部调用方法/声明对象不是正确的方法。

© www.soinside.com 2019 - 2024. All rights reserved.