java - Java 中的拼字游戏 : How to correctly write to a nested ArrayList

标签 java arraylist hashtable

我们必须编写一个基本程序,该程序应该从文件中读取单词列表,找到单词的排列,并将具有相同标准化版本的所有单词存储在一个链中。规范化版本始终位于链的顶部。 规范化的单词应用作索引键,而单词的排列应作为给定 hsah 位置中的字符串数组返回。

我们尝试通过使用嵌套的ArrayList来实现索引键和排列的存储。

 private File testfile = new File("wordlist.txt");
 private ArrayList<ArrayList<String>>[] table;
 int entries = 0;

 public Dictionary(int size) {
  table = new ArrayList[size];
  for (int i = 0; i < size; i++)
   table[i] = new ArrayList<ArrayList<String>>(99);
 }

 public void newDictionary() {
  for (int i = 0; i < table.length; i++)
   table[i] = new ArrayList<ArrayList<String>>(99);
 }

我们的哈希函数如下所示:

    public void hash(String word) {

  word = word.toLowerCase();
  String id = normalize(word);
  int hashValue = 0;
  char[] chars = word.toCharArray();

  for (int i = 0; i < chars.length; i++) {
   int e = chars[i] - 97;
   hashValue += e * 26 ^ i;
  }

  if (hashValue < 0)
   hashValue = hashValue * (-1);
  ArrayList<ArrayList<String>> chain = table[hashValue];


  boolean newList = true;
  boolean cB = chain.isEmpty();

  if (chain.size() > 0) {
   for (int i = 0; i < chain.size(); i++) {
    ArrayList<String> currentChain = chain.get(i);

    try {
     String a = currentChain.get(0);
     System.out.println(a);
    } catch (Exception e) {
     System.out.println("ERROR!");
    }

   }
  }
  if (newList == true || chain.size() == 0) {
   chain.add(new ArrayList<String>());
   chain.get(0).add(0, id);
   chain.get(0).add(word);
  }
 }

我们假设我们正确实现了嵌套的ArrayList,但是当尝试访问ArrayList<ArrayList<String>> chain = table[hashValue];时,例如通过调用boolean cB = chain.isEmpty(); ,我们的程序崩溃了。

除此之外,我们无法打印 currentChain 中索引 0 处的所有值。 。 我们用 try-catch block 包围各自的打印方法,否则我们的程序会崩溃;现在,我们的程序运行了,但很少输出字符串,而是在运行打印方法时抛出异常:

 try {
     String a = currentChain.get(0);
     System.out.println(a);
    } catch (Exception e) {
     e.printStackTrace();
    }

堆栈跟踪输出以下错误:

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.RangeCheck(ArrayList.java:547)
    at java.util.ArrayList.get(ArrayList.java:322)
    at Dictionary.hash(Dictionary.java:78)
    at Dictionary.readFromFile(Dictionary.java:32)
    at Main.main(Main.java:9)

我们对以下Index: 0, Size: 0感到非常困惑

我们是否正确实现了嵌套 ArrayList是? 大多数时候我们无法在 ArrayList 中正确存储字符串的原因可能是什么?

最佳答案

Mutimap是您正在寻找的数据结构。

A collection similar to a Map, but which may associate multiple values with a single key. If you call put(K, V) twice, with the same key but different values, the multimap contains mappings from the key to both values.

关于java - Java 中的拼字游戏 : How to correctly write to a nested ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4634782/

相关文章:

java - 如何在 Java 中以编程方式访问多维数组?

java - 为什么我在 Java String.split(regex) 中有空标记?

java - 避免在eclipse中嵌入tomcat

java - 计算文档中字符串的唯一出现次数

java - String IdentityHashMap 与 HashMap 性能对比

c - 是否可以使用 GArray 作为 GHashTable 中的值?

java - 在 Spring 中添加 Jaxb2 消息转换器打破了 Jackson2 json 映射

java - java中如何将Arraylist中的元素右移

java - 在内部存储中存储可绘制对象或位图

C++:哈希表——为什么不能编译?