Java 在一个数组中存储多个二维矩阵

Java 在一个数组中存储多个二维矩阵,java,arrays,matrix,Java,Arrays,Matrix,我试图将多个矩阵存储到一个数组中(每个矩阵都有相同的维度),我认为我的逻辑是正确的,但代码没有正确运行。我从文本文件中输入的内容如下所示(例如): 因此,在本例中,每个“”都算作一个字符,我将其转换为-1,每一新行都是一个新矩阵。我已经找到了行数和列数,并且被告知将在文本文件中看到的对数。所以我想做一个这样的数组 pairs[number of pairs][rows][columns]. 因此,在本例中,它将是成对的[3][3][10]So 3个样本,每个样本具有一个3 x 10的矩阵。我

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

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

pairs[number of pairs][rows][columns]. 
因此,在本例中,它将是成对的[3][3][10]So 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();
int行=0;
BufferedReader brr=新的BufferedReader(新文件读取器(infle));
//现在读取并存储在2D矩阵中
int[][]样本=新的int[对][行][列];
而((lines=brr.readLine())!=null){
如果(行>2){//跳过文本文件的前3行
for(int i=0;i

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

我知道发生了什么,我没有解释每一对之间的新线。因此,工作代码是:

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
BufferedReader brr=newbufferedreader(newfilereader(infle));
//现在读取并存储在2D矩阵中
int[]samples=newint[inVals.get(1)][rows][cols];
int指数=0;
而((lines=brr.readLine())!=null){
如果(curr>2){
if(行数等于(“”){
index++;//增加我们所在的对
}
对于(int j=0;j
“…但是代码没有正确运行”
——但是您忽略了告诉我们您的代码如何没有正确运行。考虑一下这一点,但是你先做了基本的调试吗?这可以通过简单地在代码中添加一组println语句来实现,这些语句告诉您变量所包含的值。而您的代码似乎混合使用了
。它声明了一个int变量
,然后给它分配了一个字符串?@HovercraftFullOfEels很抱歉,我在提交后意识到了这一点,但我编辑了代码。行只是一个计数,我可以用count=0和count++替换它,但它不会改变任何东西。我想我可能没有考虑到这两行代码之间的新代码,因为我发现我做错了什么。我会更新我的代码。
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