在java中为数组赋值时遇到问题
我不知道,但由于某种原因,数组拒绝更改,我不知道为什么。请参阅下面的进一步注释
public static void contour (int[ ][ ][]image, int rows, int cols, int maxIntensity, int[ ][ ][]newImage) throws Exception
{
PrintWriter contour = new PrintWriter("contour.ppm");
int r = 0;
int c = 0;
int a = 0;
int sumk = 0;
while (r < rows && c < cols)
{
while (a < 3)
{
image[0][r][a] = newImage[0][r][a];
image[cols-1][r][a] = newImage[cols-1][r][a];
image[c][0][a] = newImage[c][0][a];
image[c][rows-1][a] = newImage[c][rows-1][a];
a++;
}
r++;
c++;
}
System.out.println (image[32][0][2]);
System.out.println (newImage[32][0][2]);
}
此代码有点断章取义,但您应该能够看到我希望在两个数组中使哪些值相同。打印语句用于测试目的,由于某种原因我得到两个不同的值。这些数组都分配有值,但图像数组不会更改(即使我用此方法创建新数组,同样的问题仍然存在)。
您可以看到我在 main 方法中定义了这些数组,它们如下:
int image [][][] = readimage (fname, descriptor);
int newImage [][][] = new int [rows][cols][3];
因此图像数组是从不同方法返回的数组。
我是否忽略了一些非常明显的事情或者什么?我已经为此苦苦挣扎了很长一段时间,所以非常感谢所有的提示、技巧和解释!
I don't know but for some reason an array refuses to change and i have no idea why. see further comments below
public static void contour (int[ ][ ][]image, int rows, int cols, int maxIntensity, int[ ][ ][]newImage) throws Exception
{
PrintWriter contour = new PrintWriter("contour.ppm");
int r = 0;
int c = 0;
int a = 0;
int sumk = 0;
while (r < rows && c < cols)
{
while (a < 3)
{
image[0][r][a] = newImage[0][r][a];
image[cols-1][r][a] = newImage[cols-1][r][a];
image[c][0][a] = newImage[c][0][a];
image[c][rows-1][a] = newImage[c][rows-1][a];
a++;
}
r++;
c++;
}
System.out.println (image[32][0][2]);
System.out.println (newImage[32][0][2]);
}
This code is a bit out of context, but you should be able to see which values I want to make the same in both arrays. The print statements are for testing purposes, and for some reason i get two different values. These arrays both have values assigned to them, but the image array will not change (and even when I create a new array in this method the same problem persists).
You can see that I define these arrays in the main method and they are as follows:
int image [][][] = readimage (fname, descriptor);
int newImage [][][] = new int [rows][cols][3];
So the image array is a returned array from a different method.
Am I overlooking something extremely obvious or what? I have been struggling with this for quite some time, so all hints, tips and explanations are greatly appreciated!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您正在按照您想要的方式遍历列和行......
您永远不会将
a
变量重置为0
,因此您的代码只能运行一次。Assuming you are traversing the columns and rows in the way you intend to...
You never reset the
a
variable to0
, so your code only works once.如果 rows 参数或 cols 参数小于 33,则测试中打印的位置将不会从 newimage 复制到 image。 while (r < rows && c < cols) 测试会导致循环在 r 或 c 中的第一个达到最大值时终止。
If either the rows parameter or the cols parameter is less than 33, then the location printed in your test will not be copied from newimage to image. The
while (r < rows && c < cols)
test causes the loop to terminate when the first of either r or c reaches its maximum value.