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

时间:2017-12-13 19:09:19

标签: java arraylist

我需要从phase-1.txt导入到2d数组,这样文件中的每一行都会为数组的每一行放置2个变量(A String,An int)

public class Simulation {
    ArrayList fileOne;

    public ArrayList loadItem() throws Exception {

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

    public ArrayList getFileOne() {
        return fileOne;
    }
}

2 个答案:

答案 0 :(得分:0)

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

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);
}

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

的内容

答案 1 :(得分:0)

您可以创建一个类型....

for (let i = 0; i < number; i++) {
  result.push(
    <div> 
      f(i) 
      g(i) 
    </div>
  )
}