简体   繁体   English

对于Java上的幻想草稿程序,我如何将数据从文本文件加载到单独的ArrayList列表中?

[英]For a fantasy draft program on java, how would I load the data from a text file into a list of separate ArrayLists?

I'm just starting, and I'm having a hard time loading this text file into different ArrayLists . 我才刚刚开始,并且很难将这个文本文件加载到不同的ArrayLists The "------" obviously breaks each separate role. “ ------”显然打破了每个单独的角色。 I wanted to create a class of Roles and create instances for each one, but I still haven't figured out how it works properly. 我想创建一类Roles并为每个Roles创建实例,但是我仍然没有弄清楚它如何正常工作。 Also, would creating a List <Role> be better than List <ArrayList<String>> ? 另外,创建List <Role>会比List <ArrayList<String>>好吗? I know this is a basic problem, but I just can't figure it out after hours of working with it. 我知道这是一个基本问题,但经过数小时的工作后我仍然无法解决。 Thanks! 谢谢!

Leader
1   Superman    DC
2   Captain America Marvel
3   Professor X Marvel
4   The Shoveler    Mystery Men

Brawn
1   Hulk    Marvel
2   Wolverine   Marvel
3   The Thing   Marvel
4   Beast   Marvel
5   Thor    Marvel
6   Mr. Furious Mystery Men
7   Mr. Incredible  Pixar

Gadgets
1   Batman  DC
2   Iron Man    Marvel
3   Spiderman   Marvel
4   Green Lantern   DC

Female Influence
1   Wonder Woman    DC
2   Jean Gray   Marvel
3   Emma Frost  Marvel
4   Rogue   Marvel
5   Elastigirl  Pixar

Bad Guy
1   The Joker   DC
2   Magneto Marvel
3   Lex Luthor  DC
4   Dr. Doom    Marvel
5   Riddler DC
6   Syndrome    Pixar

Here's one way to do it. 这是一种方法。

Make a class that holds an integer, a string and a string. 创建一个包含整数,字符串和字符串的类。 Let's call it Person. 我们称之为人。

Now, your data structure will be HashMap<string, ArrayList<Person>> 现在,您的数据结构将为HashMap<string, ArrayList<Person>>

Have a string for the name of the list you are currently populating, ListName. 使用一个字符串表示当前正在填充的列表的名称ListName。

Have an ArrayList<Person> for the list you are currently populating, ListContents. 为您当前正在填充的列表ListContents提供一个ArrayList<Person>

Iterate over the lines of the file. 遍历文件的各行。

1) If the line is blank, skip 1)如果该行为空白,请跳过

2) If the line doesn't start with a number, we are starting a new list - insert ListContents (if it is not empty) into the hashmap under the key ListName, set ListName to this line's contents (eg "Brawn") and make ListContents a new, empty ArrayList. 2)如果该行不是以数字开头,那么我们正在启动一个新列表-将ListContents(如果它不是空的)插入到键ListName下的哈希图中,将ListName设置为该行的内容(例如“ Brawn”),然后将ListContents设为一个新的空ArrayList。

3) If it starts with a number, then split it using a regex, String.Split or whatever you like (hopefully you can split it on tabs, otherwise it would take an impossibly smart program to figure out when the spaces belong to the name and when they start belonging to the continuity!!), parse the first value as an int, and make a new Person out of these values, inserting it into the current list. 3)如果以数字开头,则使用正则表达式,String.Split或任何您喜欢的名称进行拆分(希望您可以在选项卡上拆分它,否则将需要一个不太聪明的程序来确定空格何时属于名称)当它们开始属于连续性时!),将第一个值解析为一个int,然后从这些值中创建一个新的Person,并将其插入当前列表中。

Now we have one list of people for each header, and every list is indexed by its header title in a hashmap. 现在,每个标题都有一个人员列表,并且每个列表都由其标题标题在哈希图中索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM