I have a method that is required to return an array of longs.
long[] mainMethod() {
//create the resultant array
long[] result = null;
method1(result); // Their job is to append new long values to the array
method2(result);
}
I was hoping to do something like this:
// Update the result array
int origLen = (result == null) ? 0 : result.length;
long[] newResult = new long[origLen + 4];
if (origLen != 0) {
newResult = Arrays.copyOf(result, origLen + 4);
}
newResult[origLen + 0] = someLong;
newResult[origLen + 1] = someLong;
newResult[origLen + 2] = someLong;
newResult[origLen + 3] = someLong;
result = newResult;
when I realized java passes references by value so I can't change the reference here. If cant't change the definitions of these methods (the result to be generate is to be passed as argument as some other return value exists), how can I update the original array?? I've been told NOT to use ArrayList ( I could update the methods to take ArrayList, but I was told it's silly to use ArrayList and eventually return array of longs)..
I was thinking I could initially allocate 4 long values and then keep passing it around and copying it over like:
result = Arrays.copyOf(result, origLen + 4);
I think this could work, but then how can I check if the array returned by actual, mainMethod
contains some useful info? As of now, I was checking it to be null..
Thanks in advance.