[英]How do I remove an element the user input from an ArrayList when it contains both strings and integers?
[英]How do I store 2 groups of data Integers and Strings into an 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建議的一樣
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.