获取文件数据放入2d int数组?

时间:2011-11-11 17:14:38

标签: java applet

我想获取文本文件数据,并将其放入2d int [] []数组中。我尝试了几件事,但都失败了。我怎样才能有效和正确地做到这一点?

继承我的代码:

public void readMap() throws IOException
    {
        try
        {
            in = new BufferedReader(new FileReader("testfile.txt"));
            String currLine;

            while((currLine = in.readLine()) != null)
            {
                System.out.println(currLine);

                for(int x = 0; x < tile_nums; x++)
                {
                    for(int y = 0; y < tile_nums; y++)
                    {

                    }
                }

            }   
        } 
        finally
        {
            in.close();
        }

    }

和我的文本文件数据:

100000000
111111010
100001010
111111110
000001000
000000000
000000000
000000000
000000000
000000000

1 个答案:

答案 0 :(得分:1)

情侣,

首先,为了确定outter数组的大小,您需要确定文件中的行数。或者,您可以处理每一行以生成内部数组,将它们存储在List中并从列表中创建outter数组。

其次,您需要获取每一行,确定长度(line.length())以创建适当大小的内部数组(int[] inner = new int[line.length()];

最后,将该行转换为chars数组,并使用char将每个int转换为Integer.parseInt

我不会发布整个解决方案,因为我认为这可能是功课。

相关问题