Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使列表大小不始终保持为零_Java - Fatal编程技术网

Java 如何使列表大小不始终保持为零

Java 如何使列表大小不始终保持为零,java,Java,我在乐透中有一个列表,我正在其中插入对象乐透。如下图所示 if(choice.equals("P")) { int i = 1; System.out.println("Choose a number between 1 and 90:"); List<Integer> list = new ArrayList<>(); list.add(s.nextInt()); LottoTicket lt = new LottoTicket("P

我在乐透中有一个列表,我正在其中插入对象乐透。如下图所示

if(choice.equals("P"))
{
   int i = 1;

   System.out.println("Choose a number between 1 and 90:");
   List<Integer> list = new ArrayList<>();
   list.add(s.nextInt());


   LottoTicket lt = new LottoTicket("Prima",money * 60,list, i++);
   Lotto l = new Lotto();
   l.li.add(lt);     

}
我的问题是,在乐透中我有li.size,它总是打印0,因此总是返回0,即使我已经创建了3个对象并将它们分配到列表中。下面的代码在乐透类中

List<LottoTicket> li = new ArrayList();

@Override
public void draw(){

    Random rand = new Random();     

    int[] array = new int[6];


    for (int i = 1; i < array.length ; i++) {  
        array[i] = rand.nextInt(91) + 1;
        System.out.println("Tickets sold: " + li.size());
        System.out.print(""+ array[i] + ",");
    }


}

从上面的代码片段中,我看不出您将数组的内容添加到li的位置。draw函数中的for循环仅为数组赋值,而从不向列表中添加任何内容。还值得注意的是,由于您将i=1初始化为1,因此您只生成存储在数组位置1到5中的5个随机数,如果没有更多代码,将永远无法访问数组位置0,我假设你的彩票对象不是全局的

LottoTicket lt = new LottoTicket("Prima",money * 60,list, i++);
Lotto l = new Lotto();
l.li.add(lt); 
在我看来,这里的这一部分似乎每次都在创建一个新的乐透对象。相反,为什么不将其定义为类global,这样就可以确保只有一个对象。像这样:

// Top of the class(global scope)
private Lotto mLotto = new Lotto();
然后,您可以访问该类中的任何位置,并知道您只访问1对象

另一方面,将成员暴露于外部类是非常糟糕的编码,除非您别无选择,但通常总是有选择!,在本例中,我将使用如下内容:

class Lotto {
    // define as private to avoid possible errors later on
    private List<LottoTicket> mTickets = new ArrayList<LottoTicket>();

    // define getter to return the list as a whole
    public List<LottoTicket> getTickets() { return mTickets; }

    // define an add Ticket method to add the data to the object, rather than 
    // access Lotto.li.add etc...
    public void addTicketToList(LottoTicket thisTicket){
        mTickets.add(thisTicket);
    }
}
您的思路是正确的,但是您缺少对类和代码结构的一些基本理解。希望这能帮助你找到问题的根源


显然,如果这对您没有帮助,那么您必须发布更多相关代码,以便我们能够准确地看到您的错误所在。

您可能需要发布整个方法定义。看起来您可能正在为每张彩票创建一个新的Lotto对象,而不是将其存储在任何位置,但我不能确定。可能问题是,我正在列表中插入任何内容。您能告诉我需要做什么吗?因为上面的代码就是我与此问题相关的所有代码Lottoticket lt=new LottoTicketPrima,money*60,list,i++是我想要分配给li列表的内容,我正在使用l.li.addlt添加它;li是我在Lotto类中拥有的数组列表no我想要分配的是lototicket lt=new lototicketprima,money*60,list,i++不是数组
LottoTicket lt = new LottoTicket("Prima",money * 60,list, i++);

// Replace all this
// Lotto l = new Lotto();
// l.li.add(lt);   

// with
myGlobalLottoObject.addTicketToList(lt);

// And then loop through the collected tickets with something like:
foreach(LottoTicket lt : myGlobalLottoObject.getTickets())
{
    // Do something here with each "lt" object that is returned
}