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



我正在尝试创建一个描述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;
}   
}

这是我用来从文本文件中分离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;
}   
}

如果您真的想拥有独立的id和名称数组,那么您需要有两个ArrayList。

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

然后你应该可以执行.add((.

一个更好的解决方案可能是拥有npcs 的ArrayList

public ArrayList<NPC> npcs;

然后你会像这样添加

getNPC().npcs.add(new NPC(Integer.parsInt(m.group()), m2.group()));

就像@jon skeet建议的一样

最新更新