It's not exactly possible, but you can fake it:
class Reference {
private Object value;
public Reference(Object value) {
this.value = value;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
@Override
public String toString() {
return value.toString();
}
public static Reference[] ofInts(int[] items) {
Reference[] ret = new Reference[items.length];
for(int i = 0; i < items.length; ++i) {
ret[i] = new Reference(items[i]);
}
return ret;
}
}
Then you can use it like this:
int array1[] = new int[] {1,2,3,4,5,6,7,8};
Reference refArray1[] = Reference.ofInts(array1);
final int arr2Size = 4;
Reference refArray2[] = new Reference[arr2Size];
Random rand = new Random();
for(int i = 0; i < arr2Size; ++i) {
int index = rand.nextInt(array1.length);
refArray2[i] = refArray1[index];
}
System.out.println(Arrays.toString(refArray1));
System.out.println(Arrays.toString(refArray2));
System.out.println("---");
refArray2[0].setValue(99); // set reference value
System.out.println(Arrays.toString(refArray1));
System.out.println(Arrays.toString(refArray2));
Sample output:
[1, 2, 3, 4, 5, 6, 7, 8]
[4, 6, 3, 3]
---
[1, 2, 3, 99, 5, 6, 7, 8]
[99, 6, 3, 3]
random(0,3)generate 6 as a random number? That's outside the 0-3 range.set()method can perform the appropriate update.misc.Unsafe/jni, you can't really just have an array pointing to anywhere in memory.