从二维数组中删除边

发布于 2024-12-13 01:50:32 字数 640 浏览 7 评论 0原文

我有一个数组 [300][300],我需要去掉边缘并用剩余的值填充一个新数组 [298][298]。到目前为止我已经做到了:

double[][] edging() {
   double [][] array = new double [data.length] [data[0].length];
   array = bubbles();   
   double [] [] newArray = new double[array.length-2][array[0].length-2]; 

   System.arraycopy(array,1,newArray, 0, newArray.length);

   array = newArray;
   System.out.println( + newArray.length);  
   System.out.println( + newArray[0].length);   

   return newArray;
}

这可以去除顶部和底部边缘。然而,左右依然存在。那里的检查显示新数组是 [298][300]。

我考虑过编写一个循环来依次删除每一行,对每一行使用 arraycopy 并返回新行。但不确定这是否非常有效。

尝试过这个但没有效果。下一个测试是旋转它并再次使用上面的代码。

I have an array [300][300] and I need to get rid of the edges and populate a new array [298][298] with the remaining values. I've got this far so far:

double[][] edging() {
   double [][] array = new double [data.length] [data[0].length];
   array = bubbles();   
   double [] [] newArray = new double[array.length-2][array[0].length-2]; 

   System.arraycopy(array,1,newArray, 0, newArray.length);

   array = newArray;
   System.out.println( + newArray.length);  
   System.out.println( + newArray[0].length);   

   return newArray;
}

This works to remove the top and bottom edges. However, the right and left are still there. The checks in there show me that the new array is [298][300].

I've thought about writing a loop to remove each row in turn, use arraycopy on each one and return the new row. Not sure if this would be very efficient though.

Tried this but to no avail. Next one up for a test is rotating it and using the above code again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天冷不及心凉 2024-12-20 01:50:32

干得好。您不一定要使用 System.arraycopy(),因为它仍然在 O(n^2) 运行时间内执行。在下面的程序中,我没有处理边缘情况,但这应该可以为您解决。

class MyClass {
    public static void main(String[] args) {
        MyClass myClass = new MyClass();

        int[][] sampleArray = {{11, 12, 13, 14, 15, 16},
                {17, 18, 19, 20, 21, 22},
                {23, 24, 25, 26, 27, 28},
                {29, 30, 31, 32, 33, 34},
        };

        myClass.removeEdges(sampleArray);
    }


    public int[][] removeEdges(int[][] arrayToTrim){
        int[][] newArray = new int[arrayToTrim.length-2][arrayToTrim[0].length-2];
        print2DArray(arrayToTrim);
        for(int i=1;i<arrayToTrim.length-1;i++){
            for(int j=1;j<arrayToTrim[0].length-1;j++){
                newArray[i-1][j-1] = arrayToTrim[i][j];
            }
        }
        System.out.println();
        print2DArray(newArray);
        return newArray;
    }

    private void print2DArray(int[][] anArray){
        for(int i=0;i<anArray.length;i++){
            System.out.println();
            for(int j=0;j<anArray[0].length;j++){
                System.out.print(anArray[i][j]+" ");
            }
        }
    }
}

上述程序的输出是

11 12 13 14 15 16 
17 18 19 20 21 22 
23 24 25 26 27 28 
29 30 31 32 33 34 

18 19 20 21 
24 25 26 27 

Here you go. You do not necessarily want to use the System.arraycopy() since it still performs in O(n^2) running time. In the below program, I have not handled the edge cases, but this should do it for you.

class MyClass {
    public static void main(String[] args) {
        MyClass myClass = new MyClass();

        int[][] sampleArray = {{11, 12, 13, 14, 15, 16},
                {17, 18, 19, 20, 21, 22},
                {23, 24, 25, 26, 27, 28},
                {29, 30, 31, 32, 33, 34},
        };

        myClass.removeEdges(sampleArray);
    }


    public int[][] removeEdges(int[][] arrayToTrim){
        int[][] newArray = new int[arrayToTrim.length-2][arrayToTrim[0].length-2];
        print2DArray(arrayToTrim);
        for(int i=1;i<arrayToTrim.length-1;i++){
            for(int j=1;j<arrayToTrim[0].length-1;j++){
                newArray[i-1][j-1] = arrayToTrim[i][j];
            }
        }
        System.out.println();
        print2DArray(newArray);
        return newArray;
    }

    private void print2DArray(int[][] anArray){
        for(int i=0;i<anArray.length;i++){
            System.out.println();
            for(int j=0;j<anArray[0].length;j++){
                System.out.print(anArray[i][j]+" ");
            }
        }
    }
}

The output of the above program is

11 12 13 14 15 16 
17 18 19 20 21 22 
23 24 25 26 27 28 
29 30 31 32 33 34 

18 19 20 21 
24 25 26 27 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文