[ ][ ] 和 if 语句在代码中不起作用
[ ][ ] and if-statement not working in code
我在下面的代码中遇到了一些关于 if 语句和二维数组的问题,我在下面声明:
int[][]image =
{
{0,0,2,0,0,0,0,0,0,0,0,2},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{2,0,5,5,5,5,5,5,5,5,0,2},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,2,0,0,0,0,0,0,0}//assume this rectangular image
};
int[][]smooth = new int[image.length][image[0].length]; //new array equal to image[][]
注意图片[][]。它是由一系列数字组成的二维数组。在它下面,我初始化了一个相同的数组:smooth[][]。 smooth[][] 的每个元素都被 8 个边界元素加上自身的数值平均值替换。
smooth[][] 中的边缘元素(数组外边界上的元素)不应更改。
我尝试用 if 语句来做到这一点,但只成功了一半。上边框和左边框的数字不变(r == 0 || c == 0),但下边框或右边框的任何数字都会变化成为平均值。
//compute the smoothed value of non-edge locations in smooth[][]
for(int r=0; r<image.length-1; r++)
{// x-coordinate of element
for(int c=0; c<image[r].length-1; c++)
{ //y-coordinate of element
int sum1 = 0;//sum of each element's 8 bordering elements and itself
if(r == 0 || c == 0 || r == (image[c].length) || c == (image[r].length))
smooth[r][c] = image[r][c];
else
{
sum1 = image[r-1][c-1] + image[r-1][c] + image[r-1][c+1]
+ image[r][c-1] + image[r][c] + image[r][c+1] +image[r+1][c-1]
+ image[r+1][c] + image[r+1][c+1];
smooth[r][c]= sum1 / 9; //average of considered elements becomes new elements
看来问题出在这里
if(r == 0 || c == 0 || r == (image[c].length) || c == (image[r].length))
smooth[r][c] = image[r][c];
这是你在说单元格是否是边的地方 (r == 0 || c == 0 || ...)
但你犯了一个小错误。请记住最后一个元素是长度减一,例如 (... r == (image[c].length - 1) || c == (image[r].length - 1))
.
炫酷的奖励视觉效果:
(array.length) is this ─┐ (notice it doesn't exist)
(array.length - 1) is this ─┐ │
v v
array | [x] [x] [x] [x] [x]
element is | 1st 2nd 3rd 4th 5th
index is | 0 1 2 3 4
Edit: The below is all true but I was thrown off by your use of the word equal but moreso my hasty reading. Because it is true and correct I will leave it, but it is irrelevant to this question.
int[][]smooth = new int[image.length][image[0].length]; //new array equal to image[][]
这不是创建一个与其相等的新数组,而是创建一个具有相同大小的新数组。它将新数组中的所有元素初始化为 0,而不是你认为它在做什么。使用 System.arraycopy()
的一些 for
循环来正确复制。
您过早地停止了处理,因为您的 if
语句无法捕捉到右边界和下边界的情况。您的 for
循环条件:
for(int r=0; r<image.length-1; r++)
{// x-coordinate of element
for(int c=0; c<image[r].length-1; c++)
{ //y-coordinate of element
在到达右列或底行之前停止处理。 0
的默认值恰好与由于所有零而存在的平均值相匹配。
通过更改 for
循环条件以包含右侧和底部边界情况,让您的 if
语句捕获边界情况 - 不要从长度中减去 1
。
for(int r=0; r<image.length; r++)
{// x-coordinate of element
for(int c=0; c<image[r].length; c++)
{ //y-coordinate of element
但是您必须正确测试您的 "on the right" 和 "on the bottom" 条件。从此处数组的 length
中减去一个。
if(r == 0 || c == 0 || r == (image[c].length - 1) || c == (image[r].length - 1))
smooth[r][c] = image[r][c];
我在下面的代码中遇到了一些关于 if 语句和二维数组的问题,我在下面声明:
int[][]image =
{
{0,0,2,0,0,0,0,0,0,0,0,2},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{2,0,5,5,5,5,5,5,5,5,0,2},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,5,5,5,5,5,5,5,5,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,2,0,0,0,0,0,0,0}//assume this rectangular image
};
int[][]smooth = new int[image.length][image[0].length]; //new array equal to image[][]
注意图片[][]。它是由一系列数字组成的二维数组。在它下面,我初始化了一个相同的数组:smooth[][]。 smooth[][] 的每个元素都被 8 个边界元素加上自身的数值平均值替换。
smooth[][] 中的边缘元素(数组外边界上的元素)不应更改。
我尝试用 if 语句来做到这一点,但只成功了一半。上边框和左边框的数字不变(r == 0 || c == 0),但下边框或右边框的任何数字都会变化成为平均值。
//compute the smoothed value of non-edge locations in smooth[][]
for(int r=0; r<image.length-1; r++)
{// x-coordinate of element
for(int c=0; c<image[r].length-1; c++)
{ //y-coordinate of element
int sum1 = 0;//sum of each element's 8 bordering elements and itself
if(r == 0 || c == 0 || r == (image[c].length) || c == (image[r].length))
smooth[r][c] = image[r][c];
else
{
sum1 = image[r-1][c-1] + image[r-1][c] + image[r-1][c+1]
+ image[r][c-1] + image[r][c] + image[r][c+1] +image[r+1][c-1]
+ image[r+1][c] + image[r+1][c+1];
smooth[r][c]= sum1 / 9; //average of considered elements becomes new elements
看来问题出在这里
if(r == 0 || c == 0 || r == (image[c].length) || c == (image[r].length))
smooth[r][c] = image[r][c];
这是你在说单元格是否是边的地方 (r == 0 || c == 0 || ...)
但你犯了一个小错误。请记住最后一个元素是长度减一,例如 (... r == (image[c].length - 1) || c == (image[r].length - 1))
.
炫酷的奖励视觉效果:
(array.length) is this ─┐ (notice it doesn't exist)
(array.length - 1) is this ─┐ │
v v
array | [x] [x] [x] [x] [x]
element is | 1st 2nd 3rd 4th 5th
index is | 0 1 2 3 4
Edit: The below is all true but I was thrown off by your use of the word equal but moreso my hasty reading. Because it is true and correct I will leave it, but it is irrelevant to this question.
int[][]smooth = new int[image.length][image[0].length]; //new array equal to image[][]
这不是创建一个与其相等的新数组,而是创建一个具有相同大小的新数组。它将新数组中的所有元素初始化为 0,而不是你认为它在做什么。使用 System.arraycopy()
的一些 for
循环来正确复制。
您过早地停止了处理,因为您的 if
语句无法捕捉到右边界和下边界的情况。您的 for
循环条件:
for(int r=0; r<image.length-1; r++)
{// x-coordinate of element
for(int c=0; c<image[r].length-1; c++)
{ //y-coordinate of element
在到达右列或底行之前停止处理。 0
的默认值恰好与由于所有零而存在的平均值相匹配。
通过更改 for
循环条件以包含右侧和底部边界情况,让您的 if
语句捕获边界情况 - 不要从长度中减去 1
。
for(int r=0; r<image.length; r++)
{// x-coordinate of element
for(int c=0; c<image[r].length; c++)
{ //y-coordinate of element
但是您必须正确测试您的 "on the right" 和 "on the bottom" 条件。从此处数组的 length
中减去一个。
if(r == 0 || c == 0 || r == (image[c].length - 1) || c == (image[r].length - 1))
smooth[r][c] = image[r][c];