2014-09-28 34 views
-1

我想将输入存储在我的字典类中,以便我可以搜索此类中的单词。但我也需要在其他类中使用该数组。有没有人有我如何添加输入到我的Dictionary()构造函数的想法?如何在多个类中使用数组列表?

有没有人有如何解决这个问题的想法?

非常感谢你提前!!!!

public class Dictionary { 
    // Private data fields 
    public ArrayList<String> dict; 

    Dictionary() { 
    dict = new ArrayList<String>(); 

    } 

    public void add (String s){ 
    dict.add(s); 
    } 

    public int size(){ 
    return dict.size(); 
    } 

    public String get(int i) { 
    return dict.get(i); 
    } 

    public ArrayList<String> getList(){ 
    return dict; 
    } 

    public static void main(String[] args) throws IOException { 
    if (args.length < 1) { 
     System.out.print("Must have a file."); 
     System.exit(1); 
    } 

    try { 
     File dictionaryFile = new File(args[0]); 

     Scanner fin = new Scanner(dictionaryFile); 

     System.out.println(dictionaryFile.getAbsolutePath()); 

     if (!dictionaryFile.exists()) { 
     System.out.println("Dictionary " + args[0] + "does not exist"); 
     } 

     else if (!dictionaryFile.canRead()) { 
     System.out 
      .println("Dictionary " + args[0] + " cannot be read."); 
     } 
     Dictionary dict = new Dictionary(); 
     while (fin.hasNext()) { 
     dict.add(fin.nextLine()); 
     } 

     for (int i = 0; i < dict.size(); i++) { 
     System.out.println(dict.get(i)); 
     } 

    } catch (FileNotFoundException e) { 
     System.out.println("No such file found " + args[0]); 
    } 
} 
} 

/** 
*This is the class I want to reference the array in 
*/ 
public class FindWords { 

    public static void main(String[] args) { 
    Dictionary dictionary = new Dictionary(); 
    System.out.print(dictionary.getList()); 

    } 
} 
+0

在哪个班,你所面临的麻烦访问ArrayList中之前修改您的findwords类如下

 public class FindWords { Dictionary dict=null; public void (Dictionary dict){ this.dict = dict; } public void print(){ for(int i=0;i<dict.getList.size();i++) { System.out.println(dict.getList.get(i)); } 

}

? – 2014-09-28 04:34:14

+0

我希望能够在另一个名为FindWords的类中使用字典ArrayList。但是,我要么无法引用该词典,要么将其打印为空列表。 – Theodore 2014-09-28 04:38:25

+0

可能在您的FindWords类中存在一些问题。更好地显示代码 – 2014-09-28 04:43:23

回答

-1

好......问题是,你正在创建此类的新字典对象和你的数组列表与对象关联,或者是在对象级别定义。

因此,每次实例化Dictionary对象时都会创建一个新的ArrayList。

尝试使您的Dictionary类中的arraylist为静态。

例如,

public static ArrayList<String> yourArrayList; 

现在,一旦你已经在主要方法加入你的字典一些元素,你可以从FindWord类访问数组列表,像

Dictionary.dict 

,或者,如果您无法在字典类中做出使数组列表静态的决定,因此您需要在FindWord类中初始化列表,因为您的FindWord类代码显示您没有在字典列表中添加元素。

在这里,让我再试一次。

为什么null

public class: FindWords { 
    public static void main(String[] args) { 
     Dictionary dictionary = new Dictionary();//1 

     /*** 
     . your list initialization code should come here 
     . It could be a code block or a method defined in Dictionary class itself 
     . which can be called like dictionary.initializeList(); 
     ***/ 
     System.out.print(dictionary.getList()); //3 

    } 
} 

这里,字典对象有一个与它关联的数组列表(true),但尚未初始化。所以,当你说

 dictionary.getList(); 

它实际上是提取你未初始化的列表。

+0

downvoting没有评论是粗鲁的。 – Tirath 2014-09-28 04:59:51

+0

嘿Tirath!非常感谢你 - 不幸的是,当我这样做时,输出为空。我应该把公共静态ArrayList yourArrayList; ???也许,我把它放在错误的路线上? – Theodore 2014-09-28 05:25:05

+0

@Theodore你有在Dictionary和FindWords类中定义的主要方法(你已经共享的代码)。你能告诉我,你想在Dictionary类中定义的数组列表中填充字典项吗? – Tirath 2014-09-28 06:00:06

-1

只是fininshing你的try块添加以下代码行

 FindWords f = new FindWords(dict); 
    f.print(); 
相关问题