Passing an existing array gives different results than passing a new array with elements from that array

I am programming an Android game. The color of game elements is specified through ColorTheme objects that store RGBA values. During initialization, for example, a triangle for the constructor is passed an array with RGBA values ​​from the ColorTheme object. Although the Colors in the ColorTheme-Object never change after initialization, the color of the triangle. I'm trying to figure out why. I noticed that it works as if I want to pass a new array with elements from the ColorTheme-Array, rather than passing the ColorTheme object itself into the triangle constructor. It really doesn't matter, because there is no such thing as pointers in Java (right?).

@Override
public void onSurfaceChanged(GL10 gl10, int width, int height) {

//...

mThemes = new ColorTheme[]{
            new ColorTheme(
                    new float[]{0.20f, 0.71f, 0.91f, 1.00f},    // blue circle
                    new float[]{1.00f, 1.00f, 1.00f, 1.00f},    // white obstacle
                    new float[]{1.00f, 1.00f, 1.00f, 1.00f},    // white triangle
                    new float[]{0.00f, 0.60f, 0.80f, 1.00f}     // shadow
            ),
            new ColorTheme(
                    new float[]{0.27f, 0.40f, 0.80f, 1.00f},    // purple circle
                    new float[]{1.00f, 1.00f, 1.00f, 1.00f},    // white obstacle
                    new float[]{1.00f, 1.00f, 1.00f, 1.00f},    // white triangle
                    new float[]{0.60f, 0.20f, 0.80f, 1.00f}     // shadow
            ),
            new ColorTheme(
                    new float[]{0.60f, 0.80f, 0.00f, 1.00f},    // green circle
                    new float[]{1.00f, 1.00f, 1.00f, 1.00f},    // white obstacle
                    new float[]{1.00f, 1.00f, 1.00f, 1.00f},    // white triangle
                    new float[]{0.40f, 0.60f, 0.00f, 1.00f}     // shadow
            )
    };

//...

// Values keep changing after initialization like this    
mShadowTriangle = new Triangle(mScreenRatio, mThemes[outerThemeIndex].theme[3],true);

// They don't like this 
mShadowTriangle = new Triangle(mScreenRatio, new float[]{mThemes[outerThemeIndex].theme[3][0],mThemes[outerThemeIndex].theme[3][1],mThemes[outerThemeIndex].theme[3][2],mThemes[outerThemeIndex].theme[3][3]},true);
}

      

+3


source to share


2 answers


There is no such thing as a pointer per se, but there are still object references, and they act like pointers (except that they do not allow pointer arithmetic). This means that there is all the difference in the world between

new Blah(x);

      

and

new Blah(copyOfX);

      

Each time, if these are the objects we are talking about, they are passed by reference. This means that if the constructor Blah

decides to make changes to the object that will be passed to it, then the first one will be modified with x

, and the second one will not, because only the copy will be changed.



The bottom line is, if you have an array that you don't want to communicate with, and you pass it to code that can modify the array it receives, you want to pass a clone, not the original.

If you have an array of primitives (say int[]

) you can use

int[] copyOfX = Arrays.copyOf(x, x.length);

      

to get a clone. Keep in mind that if the elements of the array are themselves objects, then this will give you a shallow copy (and you will need to look for the difference between a shallow copy and a deep copy).

+3


source


Java arrays, even primitive arrays, Object

s. As a consequence, if you don't pass a copy (using your method or one of the methods Arrays.copyOf()

), then changes to the original link will change your link Object

.



+1


source







All Articles