0

This is a continuation from a previous thread I posted regarding filtering a 2D array. My code compiles, but when I execute it, I get the following result

[[I@6e171cd7

IIRC this is a memory location, but why is it coming up from my program? I didn't get any compiler errors, and everything seems in order, but I get this alpanumeric message. Nothing bad, but also maybe not good? I'm still a greenhorn with Java, so maybe this is something common?

Here's my code:

     public static void main(String[] args) throws IOException {
             Scanner scanner = new Scanner(new File("imagedata.txt"));

             int r = scanner.nextInt();
             int c = scanner.nextInt();
             int [][] imageData = new int[r][c];
             //for loop to create 2d array from image data
             for(int row=0; row<imageData.length; row++) {
                     for(int col=0; col<imageData[row].length; col++) {
                             imageData[row][col] = scanner.nextInt();
                     }
             }

             int[][] filter1 = applyFilter1(imageData);//call return from applyFilter1
                    System.out.print(filter1);

     }

     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++){
                int[] nebs = getNeighbors(row, col, imageData);
                int sum = 0;
                for(int i=0; i<nebs.length; i++){
                    sum = sum + nebs[i];
                    int average = sum / nebs.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];
                        }
                    }
                } 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;
                    }
                }
}
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Last line in the `main` method is printing this memory location I think. This one `System.out.print(filter1);` – Osama A.Rehman Mar 29 '21 at 04:36
  • It is not printing a "memory location". That is the array object's identity hashcode. See the dup link for the correct way to turn an array into a string for printing. – Stephen C Mar 29 '21 at 05:36

0 Answers0