Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is ArrayUtils.add not adding the element here

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
like image 881
barnhillec Avatar asked Dec 06 '22 11:12

barnhillec


1 Answers

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.

like image 153
Thilo Avatar answered Dec 15 '22 00:12

Thilo