【问题标题】:Store multiple 2D matrices in an array将多个二维矩阵存储在一个数组中
【发布时间】:2017-04-15 21:45:01
【问题描述】:

我正在尝试将多个矩阵存储到一个数组中(每个矩阵具有相同的维度),我认为我的逻辑是正确的,但代码运行不正确。我从文本文件中输入的内容如下所示(例如):

0 0 0 0 0 
 0 0 0 0 0 
0 0 0 0 0

0000000000
00      00
0000000000

000 00 000
00 000 00 
000 00 000 

因此,在示例中,每个 ' ' 都算作一个字符,我将其转换为 -1,并且每个新行都是一个新矩阵。我已经找到了行数和列数,并被告知我将在文本文件中看到的对数。所以我想做一个这样的数组

pairs[number of pairs][rows][columns]. 

所以在这个例子中,它是pairs[3][3][10],所以3个样本,每个样本都有一个3 x 10矩阵。我的代码是:

int lines = 0;
BufferedReader brr = new BufferedReader(new FileReader(inFile));
        //now read and store in 2D matrix
        int[][][] samples = new int[pairs][rows][cols];
        while((lines = brr.readLine()) != null) {
            if (line > 2) { // to skip first 3 lines of text file
                for (int i = 0; i < pairs; i++) {
                    for (int j = 0; j < rows; j++) {
                        for (int k = 0; k < lines.length(); k++) {
                            //Cycle through each character in line
                            if (lines.charAt(k) == 'O' || lines.charAt(k) == '0') {
                                samples[i][j][k] = 1; //store in the matrix as a 1
                            }
                            else if (lines.charAt(k) == ' ') {

                                samples[i][j][k] = -1;
                                //store spaces as -1
                            }

                        }
                    }
                }
            }
            line++;
        }
        br.close();

抱歉,我打算写我的输出,但现在它正在分配变量,但它继续重复。我的意思是,一旦完成第三个输入,for 循环就会再次重复。换句话说,一旦 i = 2(在这个例子中),并且 j = 2,k = 2,它就会重复,并且由于某种原因,一切都会从头开始。

【问题讨论】:

  • "...but the code isn't running correctly" -- 但您忘记告诉我们如何您的代码运行不正确。考虑这样做,但首先您是否完成了基本调试?这可以通过简单地在你的代码中添加一堆 println 语句来实现,这些语句告诉你你的变量持有什么值。
  • 您的代码似乎混淆了linelines 的使用。它声明了一个 int 变量lines,然后给它分配了一个字符串?
  • @HovercraftFullOfEels 对不起,我在提交后意识到,但我编辑了代码。 line 只是一个计数,我可以用 count = 0 和 count++ 替换它,但它不会改变任何东西。我想我可能没有考虑再次查看我的代码之间的新行
  • nvm 我发现我做错了什么。我会更新我的代码。

标签: java arrays matrix


【解决方案1】:

我弄清楚发生了什么,我没有考虑每对之间的新线。所以工作代码是:

BufferedReader brr = new BufferedReader(new FileReader(inFile));
            //now read and store in 2D matrix
            int[][][] samples = new int[inVals.get(1)][rows][cols];
            int index = 0;
            while((lines = brr.readLine()) != null) {
                if (curr > 2) {                    
                    if (lines.equals("")) {
                            index++; //increment the pair we're on
                        }
                            for (int j = 0; j < rows; j++) {
                                for (int k = 0; k < lines.length(); k++) {
                                    //Cycle through each character in line
                                    if (lines.charAt(k) == 'O' || lines.charAt(k) == '0') {
                                        samples[index][j][k] = 1;
                                    }
                                    else if (lines.charAt(k) == ' ') {
                                        samples[index][j][k] = -1;
                                    }
                                }
                            }
                    }
                    curr++; //for the current line we're reading
                }
                brr.close(); //close buffered reader

【讨论】:

    猜你喜欢
    • 2022-11-30
    • 1970-01-01
    • 2020-03-19
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    相关资源
    最近更新 更多