是否可以将某个数组的某些值复制到另一个具有相同引用的数组中?

时间:2018-11-11 19:10:27

标签: java arrays reference

我认为标题是自我描述的,但让我们举个例子

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 */

有没有一种实际的方法?

1 个答案:

答案 0 :(得分:3)

对于原始类型,这是不可能的,因为数组本身存储原始值而不是引用。您需要创建自己的类,该类将封装所需的值,然后使用自定义类的元素数组。 对于类数组,存储对对象的引用,这样,您就可以实现所需的功能。