【问题标题】:How do you use an array list in multiple classes? [duplicate]如何在多个类中使用数组列表? [复制]
【发布时间】:2014-09-28 04:29:54
【问题描述】:

我想将输入存储在我的字典类中,以便我可以在该类中搜索单词。但我还需要在其他类中使用数组。有谁知道如何将输入添加到我的 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());

  }
}

【问题讨论】:

  • 您在哪个班级访问数组列表时遇到问题?
  • 我希望能够在另一个名为 FindWords 的类中使用 dict ArrayList。但是,我要么无法引用该字典,要么它打印为一个空列表。
  • 您的 FindWords 类中可能存在一些问题。更好地显示该代码
  • 这是 FindWords 的代码,无论我的原始类中的列表是什么,它都会返回一个空列表。公共类:FindWords { public static void main(String[] args) { Dictionary dictionary = new Dictionary(); System.out.print(dictionary.getList()); }
  • 您正在创建一个全新的 Dictionary 类实例。所以没有机会得到你之前在字典中插入的字符串。

标签: java arrays eclipse arraylist


【解决方案1】:

好的...问题是您正在此类中创建一个新的字典对象,并且您的数组列表与一个对象相关联,或者它是在对象级别定义的。

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

尝试在 Dictionary 类中将数组列表设为静态。

例如,

public static ArrayList<String> yourArrayList;

现在,一旦您在主方法中的 Dictionary 中添加了一些元素,您就可以从 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();

它实际上是在获取未初始化的列表。

【讨论】:

  • 在没有 cmets 的情况下投票是不礼貌的。
  • 嘿提拉斯!非常感谢 - 不幸的是,当我这样做时输出为空。我应该把 public static ArrayList yourArrayList; ???也许,我放错了行?
  • @Theodore 您在 Dictionary 和 FindWords 类中定义了 main 方法(您共享的代码)。您能告诉我您希望 Dictionary 类中定义的数组列表何时填充字典项吗?
  • 当它从文件中读取行时,我希望我的数组列表在主目录下的 Dictionary 类中填充字典项。并且只是为了查找单词(和其他类)能够引用它。太感谢了。我非常感谢。
  • @Theodore 查看我的编辑。
【解决方案2】:

修改你的 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));
       }

}

在那之后,在你的 try 块完成之前添加以下行

     FindWords f = new FindWords(dict);
    f.print();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-17
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多