如何使用println打印一个包含另一个类中字符串的数组?我的意思是:
public class questions{
public void QuestionDatabase(){
String[] QuestionArray;
QuestionArray = new String[2];
QuestionArray[0] = ("What is a dog?");
QuestionArray[1] = ("How many types of dogs are there?");
}
}
在其他课程中,我想从那里抓住一个问题:
public class quiz{
public static void main (String[] args){
//Here is where I want to grab QuestionArray[0] and print to the screen.
System.out.println("");
}
}
答案 0 :(得分:2)
从QuestionArray
:
QuestionDatabase()
public String[] QuestionDatabase(){
String[] QuestionArray;
QuestionArray = new String[2];
QuestionArray[0] = ("What is a dog?");
QuestionArray[1] = ("How many types of dogs are there?");
return QuestionArray;
}
然后像这样打印:
public class quiz{
public static void main (String[] args){
//Here is where I want to grab QuestionArray[0] and print to the screen.
System.out.println(new questions().QuestionDatabase()[0]);
}
}
答案 1 :(得分:0)
有几种方法可以做到这一点。可能最好的方法是在你的问题课中创建一个“getter”方法。此方法将简单地返回您创建的数组,如果您将该方法设为public,则可以从其他类访问它而不更改数组的值。例如:
public String[] getQuestionArray(){
return QuestionArray;
}
在您的问题类中。