java - Passing existing array gives different results than passing new array with elements out of that array -
i'm programming game android. color of game elements set through colortheme objects store rgba-values. on initialization of e.g. triangle array rgba-values out of colortheme object beingness passed constructor. although colors in colortheme-object never alter after initialization, color of triangle does. i'm trying figure out why. noticed works want if pass new array elements colortheme-array rather passing colortheme object triangle constructor. shouldn't matter because there no such thing 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}, // bluish 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}, // violet 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}, // greenish 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 maintain changing after initialization mshadowtriangle = new triangle(mscreenratio, mthemes[outerthemeindex].theme[3],true); // don't 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); }
there's no such thing pointer per se, there still object references, , deed pointers (except don't allow pointer arithmetic). means there's difference in world between
new blah(x);
and
new blah(copyofx);
each time, if these objects we're talking about, passed reference. means if constructor of blah
decides create modifications object gets passed it, first 1 end x
beingness modified, sec 1 won't, because re-create modified.
the bottom line if have array don't want messed with, , you're passing code might modify array gets given, want pass clone rather original.
if you've got array of primitives (say int[]
), can use
int[] copyofx = arrays.copyof(x, x.length);
to clone. aware if elements of array objects, though, give shallow copy (and need difference between shallow re-create , deep copy).
java android arrays
No comments:
Post a Comment