0

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!!

4
  • 3
    Please fix your code identation. Commented Nov 8, 2011 at 23:41
  • 1
    Any reason you're not using for loops for this? Have you confirmed that your loops actually affect the element at [32][0][2]? Commented Nov 8, 2011 at 23:42
  • What is newImage spose to be? The output? If so do you have your assignment statements around the wrong way? eg should be newImage[0][r][a] = image[0][r][a]; ?? Commented Nov 8, 2011 at 23:46
  • thanks for the quick reply! i got it the right way around cos i want to alter the array called image, i know the names are a bit confuisng but the reason for the name newImage comes from a different method :P a for loop doesn't work either Oli and it should work with that element, i picked a random sample within the parameters to demonstrate the error Commented Nov 8, 2011 at 23:59

2 Answers 2

2

Assuming you are traversing the columns and rows in the way you intend to... You never reset the a variable to 0, so your code only works once.

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++;
    a=0; //<----ADD THIS
}  
Sign up to request clarification or add additional context in comments.

2 Comments

haha i knew it had to be something silly, i even did something similar earlier, thanks!
Good catch. And a good example of why a for-next loop is usually more robust than a while loop.
0

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.

1 Comment

yea true, but in this case rows and cols are the same at 50, it doesnt work for any value at all which is pretty annoying :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.