2013-12-09 59 views
0

我發現了很多關於如何將ArrayList從一個類正確傳遞到另一個類的網站。我嘗試了所有這些方法,但不知何故,列表中的信息並未傳遞給其他課程。在另一個類中調用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(); 
} 
+1

你能寫也許更短的程序,只是顯示了通過類之間的名單你的問題,如果沒有額外的位?這會讓你更容易理解你的問題。 – Staven

+0

您的問題不清楚請清楚 – Deepak

回答

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(); 
    } 
    ... 
+0

它不工作的原因是什麼? –

+0

@AmanArora查看文檔:[...如果初始化需要一些邏輯(例如,錯誤處理或for循環來填充複雜數組),簡單的分配是不夠的...](http://docs.oracle.com COM/JavaSE的/教程/ JAVA/javaOO/initial.html) – alfasin

相關問題