Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用单词创建类别的最佳方法_Java_Store_Categories_Words - Fatal编程技术网

Java 使用单词创建类别的最佳方法

Java 使用单词创建类别的最佳方法,java,store,categories,words,Java,Store,Categories,Words,我目前正在做一个小刽子手项目。我想让用户能够从不同的类别中进行选择,例如国家或食品,这让我思考了处理和排序这些类别的最佳方式 我将文字保存到一个小文本文件中,该文件如下所示: Countries: Hungary Austria Argentina Canada; Food: Donut Bread Hamburger; for( ArrayList list : words ) { System.out.println( list.get(0) ); } 现在,我创建了一个多维Ar

我目前正在做一个小刽子手项目。我想让用户能够从不同的类别中进行选择,例如国家或食品,这让我思考了处理和排序这些类别的最佳方式

我将文字保存到一个小文本文件中,该文件如下所示:

Countries: Hungary Austria Argentina Canada;
Food: Donut Bread Hamburger;
for( ArrayList list : words ) {
    System.out.println( list.get(0) );
}
现在,我创建了一个多维ArrayList,它存储所有单词,每个类别在该ArrayList中的单个ArrayList中

ArrayList< ArrayList<String> > words = new ArrayList< ArrayList<String> >();

// ... read words from .txt file and store it in the words-ArrayList ...
现在我使用的这个方法非常好,对我来说似乎有点太复杂了,我想知道,是否有更简单的方法可以做到这一点。我想提前感谢您给我的任何建议。

最好用地图赚钱。映射可以是HashMap,单词category将是键,而具体形式的ArrayList列表将是相关值

然后要提取类别,您需要做的就是提取密钥集并对其进行迭代。e、 g

  Map<String, List<String>> mapList = new HashMap<String, List<String>>();

  // fill map here...

  for (String key : mapList.keySet()) {
     List<String> list = mapList.get(key);

     System.out.printf("%s: %s%n", key, list);
  }
为了我的钱最好用地图。映射可以是HashMap,单词category将是键,而具体形式的ArrayList列表将是相关值

然后要提取类别,您需要做的就是提取密钥集并对其进行迭代。e、 g

  Map<String, List<String>> mapList = new HashMap<String, List<String>>();

  // fill map here...

  for (String key : mapList.keySet()) {
     List<String> list = mapList.get(key);

     System.out.printf("%s: %s%n", key, list);
  }

类别的数量是否限制在32或64,或者你的评级者有无限数量的类别?这将是无限的,因此用户也可以添加类别,如果他愿意。类别的数量是否限制在32或64,或者你的评级者有无限数量的类别?这将是无限的,因此,如果用户愿意,也可以添加类别。
Beer: [Pilsner, Weiss, Brown_Ale, IPA]
Countries: [Hungary, Austria, Argentina, Canada]
Food: [Donut, Bread, Hamburger]