阵列的水平变换(2D) [英] Horizontal Transformation of Array (2D)

查看:81
本文介绍了阵列的水平变换(2D)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只需简单地水平和/或垂直翻转图像即可.前提是给定一个通过导入图片创建的2D整数数组,我必须创建一个具有int [] []参数的方法,并在返回void之前将其水平翻转.

It's a simple matter of flipping an image horizontally and/or vertically. The premise is that given a 2D integer array that was created from importing a picture, I must create a method with a int[][] param and horizontally flip it before returning void.

语法如下:

public static void horizontalFlip(int[][] imgArray)
    {
        int temp;
        for (int i = 0; i < imgArray.length; i++)
        {
            for (int j = 0; j < imgArray[i].length / 2; j++)
            {
                temp = imgArray[i][j];
                imgArray[i][j] = imgArray[imgArray.length - 1 - i][j];
                imgArray[imgArray.length - 1 - i][j] = temp;
            }
        }
    }

在循环交换像素时,我将imgArray用作数组参数,并使用temp作为占位符,或者这就是意图.当前,提示翻转后,窗口不执行任何操作.有人可以帮我找到逻辑或语法上的问题吗?

I use imgArray as the array param and use temp as a placeholder while the loop swaps pixels, or rather, that was the intention. Currently the window does nothing after prompting the flip. Can somebody help me find the problem with the logic or syntax?

在此先感谢您提供我应该提供的任何详细信息 P.S.我可以确认未引用的提供的代码可以正常工作并经过测试.

Thanks in advance, please specify any details I should provide P.S. I can confirm the unreferenced supplied code is functional and tested.

推荐答案

之所以发生这种情况,是因为您使用的是i而不是j.但是i不会在中途停止,而是继续并重新交换阵列.
这是正确的代码:

It is happening because you are using i instead of j. But i will not stop after halfway, but it is continued and re-swap the array.
Here is a correct code :

for (int i = 0; i < imgArray.length; i++) {
    for (int j = 0; j < imgArray[i].length / 2; j++) {
        temp = imgArray[i][j];
        imgArray[i][j] = imgArray[i][imgArray.length - 1 - j];
        imgArray[i][imgArray.length - 1 -j] = temp;
    }
}

或者如果您想交换列而不是行:

Or if you want to swap columns, not rows :

for (int i = 0; i < imgArray.length / 2; i++) {
    for (int j = 0; j < imgArray[i].length; j++) {
        temp = imgArray[i][j];
        imgArray[i][j] = imgArray[imgArray.length - 1 - i][j];
        imgArray[imgArray.length - 1 -i][j] = temp;
    }
}

这篇关于阵列的水平变换(2D)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆