java - 将文本文件行中的字符串标记输入数组,然后作为数组集合输出

标签 java arrays list arraylist iterator

我想逐行读取文本文件。文本文件中的每一行包含 5 个字符串 - 用“”分隔。从行中分割每个字符串标记后,我必须将每个标记放入一个数组中。只有这样我才能创建此类数组的数组列表。

我在打印 ArrayList 时遇到问题,因为循环似乎无限运行并且仅打印文件中最后一行(最后一个数组)上的字符串。

我可能做错了什么?

这是我的代码:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Test 
{
    static File file = new File("C:\\Users\\Nuno\\Documents\\test_file.txt");          //source file

    private static String[] values = new String[4];                                    //array of 5 values (numbers as string values)
    private static final List<String[]> ListOfArrayOfValues = new ArrayList<>();       //ArrayList of String arrays

    public static void ReadWriteArrayList()
    {
        String lines;                                                           //each line on file, that contains 5 nums

        try                                                                     //read data from file and create ArrayList containing arrays of 5 strings
        {   
            FileReader fileReader = new FileReader(file);                       //reads file in default encoding
            BufferedReader bufferReader = new BufferedReader(fileReader);       //always wrap FileReader in BufferedReader
            //lines = bufferReader.readLine();                                  //this assignment must be argument to while loop, otherwise goes in infinite loop 
            while((lines = bufferReader.readLine()) != null)       
            {
                values = lines.split(" ");                                      //assign each string token in the line (split by space) to an ARRAY of Strings
                ListOfArrayOfValues.add(values);                                //add each array to the arraylist
            }
            bufferReader.close();                                               //always close file after reading
        }catch (IOException ex) 
        {
            Logger.getLogger(DataLott.class.getName()).log(Level.SEVERE, null, ex);
        }

        //read ArrayList and output arrays (drawings) to screen
        ListIterator<String[]> outputArrayOfValues = ListOfArrayOfValues.listIterator();    //iterator for ArrayList
        while(outputArrayOfValues.hasNext())                                                //iterate through ArrayList
        {
            for(String num : values)                                            //loop each array in ArrayList
            {
                System.out.println(num + " ");                                  //print each number in a array (i.e. each number in a drawing)
            }
        }
    }

    //main method
    public static void main(String[] args) 
    {
        ReadWriteArrayList();
    }

}

输出应该与原始文件类似,例如:

10 28 31 33 34
02 15 20 29 31
10 19 23 25 34
03 04 08 33 34

相反,我得到:

03 
04 
08 
33 
34 
03 
04 
08 
33 
34 
03 
04 
08 
33 
34 
03 
04 
08 
33 
34 
03 
04 
08 
33 
34 
03 
04 
08 
33 
34 
...

最佳答案

您需要从循环内的迭代器检索数组,并更改打印值的方式:

ListIterator<String[]> outputArrayOfValues = ListOfArrayOfValues.listIterator();
while(outputArrayOfValues.hasNext())                                                    {

    // add this line to get the values from the current item in the iterator
    String[] rowValues = outputArrayOfValues.next();

    for(String num : rowValues)                                                    
    {
        //  change this line to not print a carriage return
        System.out.print(num + " ");                                  
    }

    // print the carriage return here
    System.out.println();
}

关于java - 将文本文件行中的字符串标记输入数组,然后作为数组集合输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45850277/

相关文章:

javascript - 如何添加到 React 中的嵌套数组?

c++ - C++动态数组将每次添加的大小增加1-错误

Javascript:循环遍历数组中元素的字符

c++ - 带参数 C++ 的对象数组的初始化列表

java - 在运行时从 Jar 检索文件列表并对其进行排序

java - 从多个列表生成所有组合

java - toArray 方法在扩展 ArrayList<> 的类中不起作用

java - Spring 启动: Can't Autowire Class from Other Jar Library

C++ 继承不起作用

python - 对列表中的字典元素进行排序