【问题标题】:Java How to read text file and input it into different ArrayListsJava 如何读取文本文件并将其输入到不同的 ArrayLists
【发布时间】:2017-03-29 07:23:45
【问题描述】:

我对如何读取整数文本文件并将它们作为整数放入不同的 ArrayLists 有点困惑。它们以空格分隔并填充 3 行。

例如,文本文件将具有:
1 2 3 4
5 4 3 2
3 7 8 5

而且我想把每一行放入一个 ArrayList,所以
列表[0] = [1, 2, 3, 4]
列表[1] = [5, 4, 3, 2]
列表[2] = [3, 7, 8, 5]

我当前的代码只能得到一行,我不知道如何处理它,只需要一些指导。谢谢。

我的代码:

  ArrayList<Integer> s1 = new ArrayList<Integer>();
  ArrayList<Integer> s2 = new ArrayList<Integer>();
  ArrayList<Integer> s3 = new ArrayList<Integer>();
  String line = null;

  while ((line = bufferedReader.readLine()) != null) {
    s1.add(new Integer(line));
    s2.add(new Integer(line));
    s3.add(new Integer(line));
  }

结果应该是一个列表,其中包含整数列表中的每一行

【问题讨论】:

  • 您当前的代码在哪里?请提供
  • 这个 [1, 2, 3, 4] 应该是一个字符串吗?或者你想要什么格式?
  • 作为整数。我提供了代码,我知道 s2 和 s3 得到相同的数据,但我正在测试。
  • 每行1个数组列表?
  • 每行插入ArrayList,一行的容器应该是什么,int[], String, ArrayList, ... ?

标签: java file arraylist text


【解决方案1】:

首先,您需要能够读取一行并将结果插入到列表中

String line = "1 2 3 4";
String[] array = line.split(" "); //Split the String one each Space and have the result into an Array
ArrayList<Integer> list = new ArrayList<>();
for(String value : array){ //Read the array, String by String
    list.add(Integer.valueOf(value)); //Parse and insert into the list
}

这里,list 将包含一行。 然后你需要把这个结果插入到主列表中。

ArrayList<ArrayList<Integer>> fileList = new ArrayList<>();

while ((line = bufferedReader.readLine()) != null) {
    // split the line
    fileList.add(list);
}

完整的代码如下所示:

ArrayList<ArrayList<Integer>> fileList = new ArrayList<>();

while ((line = bufferedReader.readLine()) != null) {
    String[] array = line.split(" ");
    ArrayList<Integer> list = new ArrayList<>();
    for(String value : array){
        list.add(Integer.valueOf(value));
    }

    fileList.add(list);
}

这是一个没有测试的代码,但这应该是正确的。

我已经一步一步地写下这个答案来解释当时如何解决一个问题;)

【讨论】:

    【解决方案2】:
    ArrayList<ArrayList<Integer>> s1 = new ArrayList<ArrayList<Integer>>();
            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                String[] numbers = line.split(" ");
                ArrayList<Integer> ints = new ArrayList<Integer>();
                for (String str : numbers) {
    
                    ints.add(Integer.parseInt(str));
                }
                s1.add(ints);
            }
    

    这将为您提供一个数组列表,其中包含整数类型的数组列表,其中填充了每一行的值

    【讨论】:

    • 我更喜欢在这种情况下使用Integer.valueOf(String) 直接接收 Integer,如果不是,则需要包装。不确定这是否会改变运行时的任何内容但功能是否存在;)
    • 据我了解文档没有任何区别。 Integer.valueOf(s)new Integer(Integer.parseInt(s))
    【解决方案3】:

    这段代码读入每个单词并将其放入 ArrayList:

    Scanner s = new Scanner(new File("filepath"));
    ArrayList<String> list = new ArrayList<String>();
    while (s.hasNext()){
    //As we need to add multiple tokens seperately
    list.addAll((s.next()).split(" "));
    }
    s.close();
    

    我们也可能需要在将字符串转换为整数时解析它。

    【讨论】:

    • 这段代码类似于我读一行的方式。我将如何分隔行以将其放入不同的 ArrayLists 中?
    • @James 如果你读过一行已经尝试像这样拆分它: String[] parts = string.split(" ");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-30
    • 2018-06-13
    • 2021-12-12
    • 2017-10-03
    相关资源
    最近更新 更多