2维ArrayList无法正确打印

时间:2015-05-30 20:46:30

标签: java arraylist

我正在创建一个打印mase的函数,该函数存储在2 Dimensional ArrayList<ArrayList<String>>中。但是,该功能会打印出大部分空白区域。我试图首先遍历每个ArrayList<String>,然后迭代嵌套String内的每个ArrayList<String>元素。我正在通过阅读文件创建ArrayList<ArrayList<String>>

这是文件:

XXXXXXXXSOOOOXXXXXX
XXXXXXXXXXXXOXXXXXX
XXXXXXXXXXXXOXXXXXX
XXXXXXXXXXXXOXXXXXX
XXXXXXXOOOOOXXXXXX
XXXXXXXOXXXXXXXXXXX
XXXXXXXEXXXXXXXXXXX

这是我创建它的地方:

String fileName = "maze.txt";
String line = null;
ArrayList<ArrayList<String>> lines = new ArrayList<ArrayList<String>>();

// Setup FileReader, BufferedReader, and LineReader
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);

// Get all the lines inside the maze file
while ((line = bufferedReader.readLine()) != null)
{
    ArrayList<String> lineList = new ArrayList<String>();

    for (int i = 0; i < line.length(); i++)
    {
        lineList.add(line.substring(i, i));
    }

    lines.add(lineList);                
}

这是我的功能:

public static void printMase(ArrayList<ArrayList<String>> lines)
{
    for (ArrayList<String> row: lines)
    {
        for (String elem: row)
        {
            System.out.println(elem);
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您的代码中只有一些小错误。进行以下更改。

// This is adding a blank space
lineList.add(line.substring(i, i));

// This will add a single character
lineList.add(line.substring(i, i+1));

并修改printMase(ArrayList<ArrayList<String>> lines)

public static void printMase(ArrayList<ArrayList<String>> lines)
{
    for (ArrayList<String> row: lines)
    {
        for (String elem: row)
        {
            // Using print so each character is on the same line
            System.out.print(elem);
        }
        // Now use println to end the line
        System.out.println();
    }
}

完整代码如下:

public static void main(String[] args) throws Exception {
    String fileName = "maze.txt";
    String line = null;
    ArrayList<ArrayList<String>> lines = new ArrayList<>();

    // Setup FileReader, BufferedReader, and LineReader
    FileReader fileReader = new FileReader(fileName);
    BufferedReader bufferedReader = new BufferedReader(fileReader);

    // Get all the lines inside the maze file
    while ((line = bufferedReader.readLine()) != null) {
        ArrayList<String> lineList = new ArrayList<>();

        for (int i = 0; i < line.length(); i++) {
            // Adds a single character
            lineList.add(line.substring(i, i+1));
        }

        lines.add(lineList);
    }

    printMase(lines);
}

public static void printMase(ArrayList<ArrayList<String>> lines)
{
    for (ArrayList<String> row: lines)
    {
        for (String elem: row)
        {
            // Using print so each character is on the same line
            System.out.print(elem);
        }
        // Now use println to end the line
        System.out.println();
    }
}

结果:

XXXXXXXXSOOOOXXXXXX
XXXXXXXXXXXXOXXXXXX
XXXXXXXXXXXXOXXXXXX
XXXXXXXXXXXXOXXXXXX
XXXXXXXOOOOOXXXXXXX
XXXXXXXOXXXXXXXXXXX
XXXXXXXEXXXXXXXXXXX

答案 1 :(得分:0)

Woops,我意识到我在初始化期间添加空白。我更新了我的代码,以便添加实际字符:

String fileName = "maze.txt";
String line = null;
ArrayList<ArrayList<String>> lines = new ArrayList<ArrayList<String>>();

// Setup FileReader, BufferedReader, and LineReader
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);

while ((line = bufferedReader.readLine()) != null)
{
    ArrayList<String> lineList = new ArrayList<String>();

    for (int i = 0; i < line.length(); i++)
    {
        lineList.add(Character.toString(line.charAt(i)));
    }

    lines.add(lineList);                
}