4

I use ArrayUtils.add(double[], double) with some frequency. Apparently I have a blind spot for why it's not working here. Can anyone help?

double[] reliableNeighborValues = new double[1];
for (int j = 0; j < 8; j++) {
    if (pixelInfo[j] > 0) {
        System.out.println("pre add array length "+reliableNeighborValues.length);
        for (int k = 0; k < reliableNeighborValues.length; k++) {
            System.out.println("-- "+reliableNeighborValues[k]);
        }
        ArrayUtils.add(reliableNeighborValues, pixelInfo[j]);
        System.out.println("reliable neighbor "+j+" "+pixelInfo[j]+" array length "+reliableNeighborValues.length);
        for (int k = 0; k < reliableNeighborValues.length; k++) {
            System.out.println("-- "+reliableNeighborValues[k]);
        }
    }
}

Output:

pre add array length 1

-- 0.0

reliable neighbor 0 5.3364882 array length 1

-- 0.0
2
  • you use ArrayUtils.add(double[], double) and you are using pixelInfo[j] an array in 2nd argument? Commented Oct 29, 2012 at 11:58
  • @BhavikShah: He's not passing complete array, but the value at index j. Commented Oct 29, 2012 at 12:01

1 Answer 1

15

Arrays cannot be expanded in Java, so the method returns an updated copy of the array, that you are currently discarding.

  reliableNeighborValues = 
     ArrayUtils.add(reliableNeighborValues, pixelInfo[j]);

Same deal as with the String manipulation functions.

Sign up to request clarification or add additional context in comments.

1 Comment

Good grief that's it. Thanks very much!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.