【问题标题】:Empty array when calling function(java)调用函数时的空数组(java)
【发布时间】:2015-02-14 03:42:08
【问题描述】:

所以我有这段代码可以输入内容并将其存储到列表数组中

public class manage extends admin{
public ArrayList<Game> thegame = new ArrayList<Game>();
public List<Game> ajout_jeux() { 
    boolean loop = true;

    while(loop) {
        Scanner agame = new Scanner(System.in);
        System.out.print("name: \n");
        String Cgame = agame.nextLine();
        Scanner qty = new Scanner(System.in);
        System.out.print("the qty: \n");
        int CQty = qty.nextInt();


        Console wertgame = new Console(Cgame,Cqty);
        thegame.add(new Game(Cgame,Cqty));

        System.out.println("continue?");
        Scanner autre = new Scanner(System.in);
        int continu = other.nextInt();
        if(continu==1) {

        }
        else if(continu==2) {
            Main.menu();
        }
    }

    return thegame; 
}

以及该类中应该打印数组的方法:

public void information(List<Game> thegame) {  
        System.out.print(thegame);

    }}

然后,从另一个类我需要这样称呼它

 manage management = new manage(); //the instance
 manage.information();

但是,即使我在尝试打印数组之前创建了一个对象并将其放入数组中,当我调用 manage.information(); 时,也没有错误。它只是返回一个空的 [] 列表。我不知道为什么?

这是需要调用它的类

public class themenu{
    public static void adminmenu(){

    boolean loop=true;
    while(loop){


        System.out.print("1:List items \n");
        System.out.print("4:Add \n");
        System.out.print("6:Infos \n");
        System.out.print("7:Quit \n");
        System.out.print("Choice:");
        Scanner choiceuser = new Scanner(System.in);
        String userchoix = choiceuser.nextLine();
        manage management = new manage();
        if(userchoice.equals("1")){
            manage.information(thegame);   //here I get the error
        }


        else if(userchoice.equals("4")){

                manage.ajout_jeux();

                }

谢谢

【问题讨论】:

  • 你调用information没有参数?
  • 我的错,我粘贴了错误的代码,进行了编辑。是的,我需要能够在没有参数的情况下调用信息,因为当我从我的其他类调用它时,我的数组列表没有创建/或初始化.它只是在管理类中创建
  • 为什么要为每个用户输入请求创建新的Scanner 实例?整个班级只需要一个实例。
  • 我知道,但我的问题实际上与此无关。谢谢您
  • 那不是数组,那是列表,如果声明带一个参数,你不能不带参数调用information

标签: java class object methods arraylist


【解决方案1】:

而不是调用对象的类型

 manage management = new manage(); //the instance
 manage.information();

您应该调用对象内部的方法并将数组传递给它。

management.information(new ArrayList<Game>());

【讨论】:

  • 另一件事,如果你想打印数组元素,你应该遍历这个列表,并打印每个元素。
【解决方案2】:

您需要在manage.information(); 之前调用manage.ajout_jeux,然后将该列表传递给信息方法。但请注意,当您从方法List&lt;Game&gt; 内部返回时,当前ajout_jeux 方法的返回类型是List&lt;Jeux&gt;。所以你也需要解决这个问题。方法调用将如下所示

 manage management = new manage(); //the instance
 manage.information(yourListName);

【讨论】:

  • "但是请注意,当前 ajout_jeux 方法的返回类型是 List,而您从方法 List 内部返回。所以您也需要修复它。" 我不明白是什么你的意思是。我刚开始使用 java
  • 在您的代码中将 public List ajout_jeux() 更改为 public List ajout_jeux()
  • @Phil_oneil 你可以使用一些 java IDE 进行编码
  • 好吧,它不像我想要的那样工作。实际上,我有一个直接访问ajout_jeux()的选项,它创建一个对象并将其添加到列表中。信息(列表游戏)需要打印出该列表。但是,您告诉我要做的实际上是再次要求输入并将其添加到列表中,然后再打印。但是我希望能够显示列表而无需输入新对象。谢谢
  • 我收到错误游戏无法解析为变量。请记住我是从另一个类调用它
猜你喜欢
  • 2020-07-28
  • 2022-12-19
  • 2018-10-20
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
  • 2015-08-01
相关资源
最近更新 更多