【发布时间】: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