简体   繁体   English

如何将文本文件读入2D char数组?

[英]How do I read a text file into a 2D char array?

I'm working on a school project and the objective is to take a text file that contains a maze of "#", " ", "E", and "S" characters and find your way through this maze. 我正在一个学校项目上,目标是获取一个包含迷宫字符“#”,“”,“ E”和“ S”的文本文件,并找到通过该迷宫的方法。 In the assignment it is asking for me to take the file and put each row and column into a 2D char array. 在作业中,要求我获取文件并将每一行和每一列放入2D char数组中。

Here is an example of what the file could look like("S" is where I start and "E" is where I end) : 这是文件外观的示例(“ S”是我开始的位置,“ E”是我结束的位置):

##########
# S ######
## ####E##
## #### ##
#       ##
##########

I thought about using a couple while loops one for finding the height of the maze and adding it to it's "y" component. 我考虑过使用一对while循环来查找迷宫的高度并将其添加到“ y”分量中。 Then taking each increment of "x" and putting it into the array for it's char value and reading the second y to find both "#" to find the width. 然后将“ x”的每个增量取其char值放入数组中,并读取第二个y来找到两个“#”以找到宽度。 However I so far have not implemented it correctly. 但是到目前为止,我还没有正确实现它。 Do you have any thoughts on how to go about this problem? 您对如何解决此问题有任何想法吗? So far I haven't been able to find it online. 到目前为止,我还无法在线找到它。 Also just to reiterate I need the char value so "#", " ", or "E" and "S" in the proper x and y coordinate in my 2D char array. 还要重申一下,我需要char值,以便在我的2D char数组中正确的x和y坐标中使用“#”,“”或“ E”和“ S”。

I would also provide my code of what I have but that would be considered cheating and as a student I would like to learn the content too. 我还将提供我所拥有的代码,但我认为这是作弊的,作为学生,我也想学习其内容。 So just thoughts and maybe some examples would be nice. 因此,仅凭想法和一些示例就可以了。

You can read the lines into the list of strings, and then once you know the y dimension of array, you can create 2d array and populate it with: 您可以将这些行读入字符串列表中,然后一旦知道了数组的y维,就可以创建2d数组并使用以下命令填充它:

Char[] array = line.toCharArray(); Char [] array = line.toCharArray();

Read file contents into a string. 将文件内容读入字符串。 Use x,y -> index transformation for your coordinates. 使用x,y->索引转换作为坐标。 Access the characters of this string as if the characters where in 2D array. 就像在2D数组中的字符一样访问此字符串的字符。 See a solution to the problem "Using an array of Strings to create a 2D (map) array in Java" . 请参阅“使用字符串数组在Java中创建2D(映射)数组”问题的解决方案

Personally what I would do is to divide this into a board, and have a board object. 就我个人而言,我要做的是将其分成一个板,并有一个板对象。 On the board object have number of square objects. 板上的对象有多个正方形对象。 Think of it as a chess board. 将其视为棋盘。

  |  |
__|__|__
  |  |
__|__|__
  |  |
  |  |

Each square object would have a X, Y values to say it's location and then the actual value of the square (3, #, ... etc) 每个正方形对象都有一个X,Y值来表示其位置,然后是正方形的实际值(3,#等)

public class board {

    ArrayList a<Square>;
}

public class Square {

    int x;
    int y;
    String value;
}

Then you can read your text file and build your board. 然后,您可以阅读文本文件并构建板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM