[英]How do I store 2 groups of data Integers and Strings into an ArrayList
I'm trying to make a NPC class that describes an npc. 我正在尝试制作一个描述npc的NPC类。 A single npc has an ID and has a name. 单个npc具有ID和名称。 How do I put a list of names into the names and the ids into the ids. 如何将名称列表放入名称中,并将ID放入ID中。
I seperated the names and ids from a text file. 我从文本文件中分离了名称和ID。 m.group() is the ids and m2.group() is the names. m.group()是id,m2.group()是名称。 So basically what I don't understand is how to add the groups to the list. 因此,基本上我不理解的是如何将组添加到列表中。 I tried using 我尝试使用
getNPC().npcs.add(m.group());
but doesn't work? 但是不行吗?
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReadFile {
private String path;
public NPC npc;
public NPC getNPC() {
return npc;
}
public ReadFile(String file_path) {
this.path = file_path;
}
public String[] openFile() throws IOException {
FileReader fr = new FileReader(path);
BufferedReader textReader = new BufferedReader(fr);
int numberOfLines = readLines();
String[] textData = new String[numberOfLines];
for (int i=0; i < numberOfLines; i++) {
textData[i] = textReader.readLine();
}
textReader.close();
return textData;
}
int readLines() throws IOException {
FileReader file_to_read = new FileReader(path);
BufferedReader bf = new BufferedReader(file_to_read);
String aLine;
int numberOfLines = 0;
while ((aLine = bf.readLine()) != null) {
numberOfLines++;
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(aLine);
int id = m.group().indexOf(m.group().length());
Pattern p2 = Pattern.compile("[a-z\\sA-Z]+$");
Matcher m2 = p2.matcher(aLine);
String name = m2.group();
while (m.find() && m2.find()) {
System.out.println(m.group());
System.out.println(m2.group());
getNPC().npcs.add(new NPC(id, name));
}
}
bf.close();
return numberOfLines;
}
}
This is the class I use to seperate the IDS and names from a text file. 这是我用来从文本文件中分离IDS和名称的类。
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReadFile {
private String path;
public NPC npc;
public NPC getNPC() {
return npc;
}
public ReadFile(String file_path) {
this.path = file_path;
}
public String[] openFile() throws IOException {
FileReader fr = new FileReader(path);
BufferedReader textReader = new BufferedReader(fr);
int numberOfLines = readLines();
String[] textData = new String[numberOfLines];
for (int i=0; i < numberOfLines; i++) {
textData[i] = textReader.readLine();
}
textReader.close();
return textData;
}
int readLines() throws IOException {
FileReader file_to_read = new FileReader(path);
BufferedReader bf = new BufferedReader(file_to_read);
String aLine;
int numberOfLines = 0;
while ((aLine = bf.readLine()) != null) {
numberOfLines++;
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(aLine);
int id = m.group().indexOf(m.group().length());
Pattern p2 = Pattern.compile("[a-z\\sA-Z]+$");
Matcher m2 = p2.matcher(aLine);
String name = m2.group();
while (m.find() && m2.find()) {
System.out.println(m.group());
System.out.println(m2.group());
getNPC().npcs.add(new NPC(id, name));
}
}
bf.close();
return numberOfLines;
}
}
If you really want to have separate arrays of ids and names then you need to have two ArrayList. 如果您确实希望具有ID和名称的单独数组,则需要具有两个ArrayList。
ArrayList<String> npcIds;
ArrayList<String> npcNames;
Then you should be able to do the .add(). 然后,您应该可以执行.add()。
A better solution would probably to have an ArrayList of npcs 更好的解决方案可能是使用npcs的ArrayList
public ArrayList<NPC> npcs;
And then you would add to them like so 然后您将像这样添加到他们
getNPC().npcs.add(new NPC(Integer.parsInt(m.group()), m2.group()));
Like what @jon-skeet suggested 就像@ jon-skeet建议的一样
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.