我找到了许多关于如何将ArrayList从一个类正确传递到另一个类的网站。我已经尝试了所有这些方法,但不知何故,列表中的信息不会传递给其他类。
public class Final {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean correctInput;
int numQuestions;
String again;
Part1 part1 = new Part1();
Part2 part2 = new Part2();
do{
System.out.println("Welcome to the What If game.");
do{
try{
correctInput = false;
System.out.println("How many questions and answers would you like? There's a max of 15.");
Scanner filein = new Scanner(System.in);
numQuestions = filein.nextInt();
part1.numQuestions(numQuestions);
while(numQuestions > 15){
System.out.println("Sorry, you can only have up to 15. Try again.");
numQuestions = input.nextInt();
part1.numQuestions(numQuestions);
}
}
catch(InputMismatchException e){
System.out.println("You entered in an invalid answer.");
correctInput = true;
}
}while(correctInput);
part1.Random();
System.out.println("Would you like the questions and answers printed to the screen or a separate file? Enter 'screen' or 'file'.");
String screenFile = input.next();
if(screenFile.equalsIgnoreCase("screen")){
part2.PrinttoScreen();
}
else if(screenFile.equalsIgnoreCase("file")){
System.out.println("Please input the location you want to write to. Example: C:\\temp\\WhatIf.txt");
String writeFile = input.next();
part2.writeFile(writeFile);
part2.PrinttoFile();
}
else{
System.out.println("Invalid input. Try again.");
screenFile = input.next();
}
System.out.println("Do you want to play again? y/n");
again = input.next();
again.toLowerCase();
}while(again == "y");
}
首先支持班级:
public class Part1 {
ArrayList<String> arrayQuestions = new ArrayList<String>();
ArrayList<String> arrayAnswers = new ArrayList<String>();
ArrayList<String> randomPairs = new ArrayList<String>();
int numQuestions;
public void numQuestions(int numQuestions){
this.numQuestions = numQuestions;
}
private void ReadFile1(){
try {
Scanner fileIn = new Scanner(Paths.get("C:\\temp\\Questions.txt"));
while(fileIn.hasNextLine()){
arrayQuestions.add(fileIn.nextLine());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void ReadFile2(){
try {
Scanner fileIn2 = new Scanner(Paths.get("C:\\temp\\Answers.txt"));
while(fileIn2.hasNextLine()){
arrayAnswers.add(fileIn2.nextLine());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void Random(){
ReadFile1();
ReadFile2();
Random randnum1 = new Random();
Random randnum2 = new Random();
randomPairs = new ArrayList<String>();
for(int x=0; x<this.numQuestions; x++){
int rand1 = randnum1.nextInt(arrayQuestions.size());
String randquestion =arrayQuestions.get(rand1);
randomPairs.add(randquestion);
int rand2 = randnum2.nextInt(arrayAnswers.size());
String randanswer = arrayAnswers.get(rand2);
randomPairs.add(randanswer);
}
}
public ArrayList<String> getPairs(){
return randomPairs;
}
第二支持班级:
public class Part2 {
PrintWriter fileout;
String writeFile;
Part1 part1 = new Part1();
ArrayList<String> randomPairs = part1.getPairs();
public void writeFile(String writeFile){
this.writeFile = writeFile;
}
public void PrinttoScreen(){
for(int j=0; j<randomPairs.size(); j++){
System.out.println(randomPairs.get(j));
}
}
public void PrinttoFile(){
try {
fileout = new PrintWriter(this.writeFile);
for(int x=0; x<randomPairs.size(); x++){
fileout.println(randomPairs.get(x));
}
fileout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args){
Part2 test = new Part2();
test.PrinttoScreen();
}
答案 0 :(得分:0)
班级part2
中的以下一行:
ArrayList<String> randomPairs = part1.getPairs();
不起作用 - 您应该将初始化移到构造函数中:
public class Part2 {
PrintWriter fileout;
String writeFile;
Part1 part1 = null;
ArrayList<String> randomPairs = null;
Part2(){
part1 = new Part1();
randomPairs = part1.getPairs();
}
...