所以我有这个代码输入内容并将其存储到listarray
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();
}
谢谢
答案 0 :(得分:0)
您需要在manage.ajout_jeux
之前调用manage.information();
,然后将该列表传递给信息方法。但请注意,当您从方法ajout_jeux
内部返回时,List<Jeux>
方法的返回类型目前为List<Game>
。所以你也需要解决这个问题。方法调用将如下所示
manage management = new manage(); //the instance
manage.information(yourListName);
答案 1 :(得分:0)
而不是调用对象的类型
manage management = new manage(); //the instance
manage.information();
你应该在对象中调用方法并将数组传递给它。
management.information(new ArrayList<Game>());