2015-05-15 61 views
0

因此,我一直在Java中編寫一個項目,並嘗試將數組列表引用到另一個類時遇到了暫停。第一個代碼是我試圖引用ArrayList的類。第二個是我創建ArrayList的地方。將Java文件轉換爲ArrayList並將ArrayList引用到另一個類

import Game.App; 

public class WordPuzzleGenerator { 

    public WordPuzzleGenerator() { 
     if (Game.SizeOfPuzzle.currentRows == 4){ 
      threeLetterWordList 
     }else if (Game.SizeOfPuzzle.currentRows == 5){ 
      maximum = 12482; 
     }else{ 
      maximum = 1310; 
     } 
    } 

Scanner threeLetterScanner = new Scanner(File("3LetterWords.txt")); 
    ArrayList<String> threeLetterWordList = new ArrayList<String>(); 
    while (threeLetterScanner.hasNext()){ 
     threeLetterWordList.add(threeLetterScanner.next()); 
    } 
    threeLetterScanner.close(); 

    Scanner fourLetterScanner = new Scanner(File("4LetterWords.txt")); 
    ArrayList<String> fourLetterWordList = new ArrayList<String>(); 
    while (fourLetterScanner.hasNext()){ 
     fourLetterWordList.add(fourLetterScanner.next()); 
    } 
    fourLetterScanner.close(); 

    Scanner fiveLetterScanner = new Scanner(File("5LetterWords.txt")); 
    ArrayList<String> fiveLetterWordList = new ArrayList<String>(); 
    while (fiveLetterScanner.hasNext()){ 
     fiveLetterWordList.add(fiveLetterScanner.next()); 
    } 
    fiveLetterScanner.close(); 
} 
+0

如果我理解得很好,你必須使用getters。 – Drumnbass

+2

那到底是什麼問題? – Mureinik

+0

@Mureinik正如wordPuzzleGenerator類所示,我試圖引用已經在App中創建的ArrayList的第一個if語句。 – Jackson

回答

1

使用類像內部的變量:

public class WordPuzzleGenerator { 

    ArrayList<String> threeLetterWordList = new ArrayList<String>(); 
    //... 
    public List<String> getThreeLetterWordList() { 
     return threeLetterWordList ; 
    } 
} 

那麼你可以調用myGenerator.getThreeLetterWordList();得到你的名單。

+0

啊好吧,將工作謝謝你@breakline – Jackson

相關問題