Java 如何将两组数据整数和字符串存储到ArrayList中

Java 如何将两组数据整数和字符串存储到ArrayList中,java,arrays,regex,Java,Arrays,Regex,我正在尝试制作一个描述NPC的NPC类。单个npc有一个ID和一个名称。如何将名称列表放入名称中,将ID放入ID中 我把名字和ID从一个文本文件中分离出来。m、 group()是ID,m2.group()是名称。所以基本上我不明白的是如何将组添加到列表中。我试着用 getNPC().npcs.add(m.group()); 但不起作用 import java.io.IOException; import java.io.FileReader; import java.io.BufferedR

我正在尝试制作一个描述NPC的NPC类。单个npc有一个ID和一个名称。如何将名称列表放入名称中,将ID放入ID中

我把名字和ID从一个文本文件中分离出来。m、 group()是ID,m2.group()是名称。所以基本上我不明白的是如何将组添加到列表中。我试着用

getNPC().npcs.add(m.group()); 
但不起作用

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;
}   
}
import java.io.IOException;
导入java.io.FileReader;
导入java.io.BufferedReader;
导入java.util.ArrayList;
导入java.util.regex.Matcher;
导入java.util.regex.Pattern;
公共类读取文件{
私有字符串路径;
全国人民代表大会;
公共NPC getNPC(){
返回全国人大;
}
公共读取文件(字符串文件\u路径){
this.path=文件路径;
}
公共字符串[]openFile()引发IOException{
FileReader fr=新的FileReader(路径);
BufferedReader textReader=新的BufferedReader(fr);
int numberOfLines=readLines();
字符串[]文本数据=新字符串[numberOfLines];
对于(int i=0;i
这是我用来从文本文件中分离ID和名称的类

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;
}   
}
import java.io.IOException;
导入java.io.FileReader;
导入java.io.BufferedReader;
导入java.util.ArrayList;
导入java.util.regex.Matcher;
导入java.util.regex.Pattern;
公共类读取文件{
私有字符串路径;
全国人民代表大会;
公共NPC getNPC(){
返回全国人大;
}
公共读取文件(字符串文件\u路径){
this.path=文件路径;
}
公共字符串[]openFile()引发IOException{
FileReader fr=新的FileReader(路径);
BufferedReader textReader=新的BufferedReader(fr);
int numberOfLines=readLines();
字符串[]文本数据=新字符串[numberOfLines];
对于(int i=0;i
如果您真的想拥有单独的ID和名称数组,那么您需要有两个ArrayList

ArrayList<String> npcIds;
ArrayList<String> npcNames;

比如@jon skeet建议的

以及在哪里使用getNPC().npcs.add(m.group())?我不清楚您的问题是使用正则表达式提取两个不同的值,还是如何存储它们。你的问题应该只涉及其中一个。我想说你的
NPC
类设计看起来很似是而非,就你的
列表而言。我希望在
readLines
方法中有一个
列表
。是的,您确定m.group()和m2.group()确实包含您想要的值吗?有可能你的“提取”没有提取任何东西。可能有助于添加一些示例ID/名称,以确保匹配器正常工作Yes m.group()和m2.group()正好包含我想要的内容。我刚刚更新了帖子。我只想将信息存储在m.group()和m2.group中。m、 group()只包含数字,但我不确定如何将其从字符串-->整数转换为整数。m2.group()只包含字母。(名称)我创建了一个npc类,但当我尝试创建一个新的npc时,我不能使用m.group(),因为它是字符串而不是整数。对不起,我真的把自己搞糊涂了,我得到了一个空指针。getNPC().npcs.add(新的NPC(Integer.parseInt(m.group()),m2.group());在这条线上。线程“main”中出现异常您能更新上面的代码吗?我想你把NPC类代码写得太多了。很抱歉反应太晚。。。。没有足够的上下文来理解空指针问题是什么。
public ArrayList<NPC> npcs;
getNPC().npcs.add(new NPC(Integer.parsInt(m.group()), m2.group()));