0

I think the title is self-descriptive but let's just put an example

float[] shape1 = new float[4]; //Those 4 floats are XCoord,YCoord,Width,Height
float[] shape1XY = new float[2] /*I want to store here the values of the
first 2 floats from shape1
with the ability that when i change them
shape1[0] and shape1[1] are also changed */

Is there a way of actually doing this?

  • @dasblinkenlight Sorry but...which answer there is the right one? can't really tell – Omar Abdul'Azeez Nov 11 '18 at 20:02
  • There's no "right" answer to this question, as there is no way to do what you wish to do. Hence you need to pick a work-around that is the most appropriate, or the least annoying, in your circumstances. – Sergey Kalinichenko Nov 11 '18 at 20:09

1 Answers1

3

For primitive types this is not possible as arrays store primitive values themselves not references. You need to create your own class which will incapsulate values you need and then use arrays of elements of your custom class. For classes arrays store references to the objects so in that case you will be able to achieve what you want.

Ivan
  • 8,508
  • 2
  • 19
  • 30
  • I thought like... since arrays are considered objects they are a reference to the values inside them so if `shape1XY` is a `float[4]` i can do `shape1XY=shape1` and now if i change any of the 4 values in `shape1XY` the original array will be changed as well so... why can't i take a part of that reference and not the reference to all 4 values – Omar Abdul'Azeez Nov 11 '18 at 19:25
  • @Omar Because in an object hierarchy references to leaves are different than references to branches. Leaves are immutable, branches generally not. An array would be a branch, its float contents would be leaves – Perdi Estaquel Nov 12 '18 at 04:42