Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 文件从同一行导入2个变量_Java_Arraylist - Fatal编程技术网

Java 文件从同一行导入2个变量

Java 文件从同一行导入2个变量,java,arraylist,Java,Arraylist,我需要从phase-1.txt导入到一个2d数组,这样文件中的每一行将为数组的每一行放置2个变量(一个字符串,一个int) 据我所知,你需要这样的东西: public void readFile() throws IOException { ArrayList<Object[]> rows = new ArrayList<Object[]>(); BufferedReader in = new BufferedReader(new FileReader("

我需要从phase-1.txt导入到一个2d数组,这样文件中的每一行将为数组的每一行放置2个变量(一个字符串,一个int)


据我所知,你需要这样的东西:

public void readFile() throws IOException {
    ArrayList<Object[]> rows = new ArrayList<Object[]>();
    BufferedReader in = new BufferedReader(new FileReader("phase-1.txt"));
    String line;
    while((line = in.readLine()) != null) {
        String words[] = line.split("\\s+");
        String first = words[0];
        int second = Integer.parseInt(words[1]);
        rows.add(new Object[] {first, second});
    }
    Object[][] finalRows = new Object[rows.size()][];//2d array
    rows.toArray(finalRows);
}
public void readFile()引发IOException{
ArrayList行=新的ArrayList();
BufferedReader in=新的BufferedReader(新文件读取器(“phase-1.txt”);
弦线;
而((line=in.readLine())!=null){
字符串字[]=行。拆分(\\s+);
第一个字符串=单词[0];
int second=Integer.parseInt(字[1]);
add(新对象[]{first,second});
}
Object[][]finalRows=新对象[rows.size()][];//二维数组
行。到阵列(最终行);
}

对于数组,您可以使用类似ArrayList或Object[][]

的内容,您可以创建一个类型

public class Simulation {
    ArrayList fileOne;

    public ArrayList loadItem () throws Exception{

        File phaseOne = new File("phase-1.txt");
        fileOne = new ArrayList<FileRow>();
        Scanner scanner1 = new Scanner(phaseOne);
        while(scanner1.hasNextLine()){
            fileOne.add(new FileRow(scanner1.nextLine()));
        }
       return fileOne;
    }

    public ArrayList getFileOne() {
        return fileOne;
    }

}

class FileRow{
    String stringValue;
    int intValue;

    FileRow(String fileRowString){
        String [] splitString = fileRowString.split("=");
        this.stringValue = splitString[0];
        this.intValue = splitString[1];
    }
}
公共类模拟{
ArrayList fileOne;
公共ArrayList loadItem()引发异常{
文件phaseOne=新文件(“phase-1.txt”);
fileOne=新的ArrayList();
扫描仪scanner1=新扫描仪(phaseOne);
while(scanner1.hasNextLine()){
添加(新文件行(scanner1.nextLine());
}
返回fileOne;
}
公共阵列列表getFileOne(){
返回fileOne;
}
}
类文件行{
字符串字符串值;
int值;
文件行(字符串文件行字符串){
String[]splitString=fileRowString.split(“”);
this.stringValue=splitString[0];
this.intValue=splitString[1];
}
}

好的,那么你的问题是什么呢?考虑到文件中的每一行都包含一个字符串和int(例如,
test 123
),你可以将它作为一个组合字符串存储在列表中,在访问时,将其拆分并按各自的类型强制转换。嗨,osoblanco,我尝试过你的方法,但我发现它没有完全工作,因为我的文件行看起来像那样(建筑工具=3000)所以我需要用等号分割这条线,这样等号之前的所有内容都应该放在字符串中,等号之后的所有内容都应该放在整数值中。我更新了解决方案来解决这个额外的信息。同时,如果它是一个真正的等号关系,没有重复的字符串,顺序无关紧要,我会在不要创建FileRow类型。谢谢你,osoblanco:)非常感谢我,Sean,谢谢你分享你的答案。我需要添加到2d arraylist,但我以前从未使用过bufferedreader。如何获得(in)的大小以帮助填充数组。如果您不知道文件中有多少行,那么您只能通过读取整个文件来了解。因此,您可以只使用根据需要增长的列表。我正在更新答案。谢谢你,肖恩,解决方案很有效
public class Simulation {
    ArrayList fileOne;

    public ArrayList loadItem () throws Exception{

        File phaseOne = new File("phase-1.txt");
        fileOne = new ArrayList<FileRow>();
        Scanner scanner1 = new Scanner(phaseOne);
        while(scanner1.hasNextLine()){
            fileOne.add(new FileRow(scanner1.nextLine()));
        }
       return fileOne;
    }

    public ArrayList getFileOne() {
        return fileOne;
    }

}

class FileRow{
    String stringValue;
    int intValue;

    FileRow(String fileRowString){
        String [] splitString = fileRowString.split("=");
        this.stringValue = splitString[0];
        this.intValue = splitString[1];
    }
}