简体   繁体   中英

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. A single npc has an ID and has a name. How do I put a list of names into the names and the ids into the ids.

I seperated the names and ids from a text file. m.group() is the ids and m2.group() is the names. 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.

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.

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

Then you should be able to do the .add().

A better solution would probably to have an ArrayList of npcs

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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