简体   繁体   English

使用方法调用打印出过滤后的二维数组时遇到问题

[英]Having trouble printing out a filtered 2d array with a method call

EDIT: fixed issues in the code, but new, novel error has occurred.编辑:修复了代码中的问题,但出现了新的、新颖的错误。 Posting to new thread for insight.发布到新线程以获取见解。

I'm having some issues with a homework assignment that needs me to print out a filtered copy of a 2d array.我的家庭作业有一些问题,需要我打印出二维数组的过滤副本。 I need to create a nested for-loop that calls another method(which stores the neighbors of an index in a 1D array), finds the average of the 1D array, and then prints out a 2D array with the averages.我需要创建一个嵌套的 for 循环,该循环调用另一个方法(它将索引的邻居存储在一维数组中),找到一维数组的平均值,然后打印出一个带有平均值的二维数组。

I was able to code the method "getNeighbors" to obtain the neighbors of an index, but when I call the method into the main, I get 6 errors:我能够编写方法“getNeighbors”来获取索引的邻居,但是当我将该方法调用到 main 中时,我得到了 6 个错误:

Lab5.java:45: error: cannot find symbol getNeighbors(row, col, imageData); Lab5.java:45: 错误: 找不到符号 getNeighbors(row, col, imageData); ^ symbol: variable imageData location: class Lab5 ^ 符号:可变图像数据位置:class Lab5

Lab5.java:47: error: cannot find symbol for(int i=0; i<nebs.length; i++){ ^ symbol: variable nebs location: class Lab5 Lab5.java:47: 错误: 找不到符号 (int i=0; i<nebs.length; i++){ ^ 符号: 变量 nebs 位置: class Lab5

Lab5.java:48: error: cannot find symbol sum += nebs[i]; Lab5.java:48: 错误: 找不到符号 sum += nebs[i]; ^ symbol: variable nebs location: class Lab5 ^ 符号:可变 nebs 位置:class Lab5

Lab5.java:50: error: cannot find symbol int average = sum / nebs[i].length; Lab5.java:50: 错误: 找不到符号 int average = sum / nebs[i].length; ^ symbol: variable nebs location: class Lab5 ^ 符号:可变 nebs 位置:class Lab5

Lab5.java:50: error: cannot find symbol int average = sum / nebs[i].length; Lab5.java:50: 错误: 找不到符号 int average = sum / nebs[i].length; ^ symbol: variable i location: class Lab5 ^ 符号:变量 i 位置:class Lab5

Lab5.java:65: error: incompatible types: int[][] cannot be converted to int[] } return copyImage; Lab5.java:65: 错误: 不兼容类型: int[][] 不能转换为 int[] } return copyImage;

I don't know if it's mental fatigue getting to me, but I can't figure out how to properly call the method to provide the 1D neighbors array and then get the average without using variables from the getNeighbors method.我不知道这是否对我造成了精神疲劳,但我不知道如何正确调用该方法来提供一维邻居数组,然后在不使用 getNeighbors 方法中的变量的情况下获取平均值。

Here is my code:这是我的代码:


public static int[][] applyFilter1(int[][] imageData){
        int[][] filtered = new int[imageData.length][imageData[0].length];

        for (int row=0; row<filtered.length;row++){
            for (int col=0;col<filtered[row].length;col++){
                getNeighbors(row, col, imageData);
                int sum = 0;
                for(int i=0; i<nebs.length; i++){
                    sum += nebs[i];
                 }
                 int average = sum / nebs[i].length;
                 filtered[row][col] = average;
                }
            }
            return filtered;
        }

        public static int[] getNeighbors(int row, int col, int[][] imageData){
            //find neighbors of current index
            int [][] copyImage = new int[imageData.length][imageData[0].length];
                try{
                    for(int r=0; r<imageData.length; r++){
                        for(int c=0; c<imageData[r].length; c++){
                            imageData[r][c] = copyImage[r][c];
                        }
                    } return copyImage;
                } catch(Exception e){
                    System.out.println("Array copy not successful");
                }

                    //handles the top row of the array
                    if(row==0){//if copyImage[0].length
                        if(col==0){//handles upper left corner of array
                            int[] nebs = {copyImage[row][col], copyImage[row+1][col],
                                                        copyImage[row][col+1]};
                            return nebs;
                        }
                        else if(col==copyImage[row].length-1){//handles upper right corner of array
                            int[] nebs = {copyImage[row][col], copyImage[row][col-1],
                                                        copyImage[row+1][col]};
                            return nebs;
                        }
                        else{//handles top row of array between corners
                            int[] nebs = {copyImage[row][col], copyImage[row][col-1],
                                                        copyImage[row+1][col], copyImage[row][col+1]};
                            return nebs;
                        }
                    }
                    //handles the bottom row of the array
                    else if(row==copyImage.length-1){//if the row is at max value
                        if(col==0){//handles botton left corner of array
                            int[] nebs = {copyImage[row][col], copyImage[row-1][col],
                                                        copyImage[row][col+1]};
                            return nebs;
                        }
                        else if(col==copyImage[row].length-1){//handles bottom right corner of array
                            int[] nebs = {copyImage[row][col], copyImage[row-1][col],
                                                        copyImage[row][col-1]};
                            return nebs;
                        }
                        else{//handles bottom row of array
                            int[] nebs = {copyImage[row][col], copyImage[row-1][col],
                                                        copyImage[row][col-1], copyImage[row][col+1]};
                            return nebs;
                        }
                    }
                    //handles leftmost column of array
                    else if(col==0){//if col=0 and row increases
                        int[] nebs = {copyImage[row][col], copyImage[row-1][col],
                                                    copyImage[row+1][col], copyImage[row][col+1]};
                        return nebs;
                    }
                    //handles rightmost column of array
                    else if(col==copyImage[row].length-1){//if col=max value and row increases
                        int[] nebs = {copyImage[row][col], copyImage[row-1][col],
                                                    copyImage[row+1][col], copyImage[row][col-1]};
                        return nebs;
                    }
                    //handles values in the body of the array
                    else{
                        int[] nebs = {copyImage[row][col], copyImage[row-1][col],
                                                    copyImage[row+1][col], copyImage[row][col-1],
                                                    copyImage[row][col+1]};
                        return nebs;
                    }
                }

What can I do to get this to compile and execute correctly?我该怎么做才能使它正确编译和执行?

Perhaps you meant to write image (the name of the int[][] parameter to applyFilter1 ) in your getNeighbors call, rather than imageData ?也许您打算在getNeighbors调用中写入imageint[][]参数的名称applyFilter1 ),而不是imageData And probably you also meant to save the result from getNeighbors in an int[] called nebs ?也许您还打算将 getNeighbors 的结果保存在名为getNeighborsint[] nebs

    int[][] nebs = getNeighbors(row, col, image);

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

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