2

I would like to concatenate an array in Java by reference like so.

int[] a = new int[] {1,2,3};
int[] b = new int[] {4,5,6};
int[] ab = concatArrayByReference(a,b); //Function to be defined

now if I do

a[2] = 90;

ab should be equal to {1,2,90,4,5,6}

Is this possible in Java, and what about splitting an array by reference?

I want this mostly for performance reasons, I would like to avoid copying the two extremely big arrays each time I concatenate them.

Edit: it seems it is not possible in Java, then how would I go about making a high performance implementation of List to make this possible?

Bloc97
  • 274
  • 1
  • 13
  • 1
    What do you think you mean by "by reference"? – Andy Turner Mar 12 '17 at 16:39
  • a[0] should be referring to the same location in memory as ab[0] – Bloc97 Mar 12 '17 at 16:40
  • 1
    And no, you can't do this at all with arrays. You can only do this with a special implementation of `List`. – Andy Turner Mar 12 '17 at 16:40
  • Possible duplicate of [How can I concatenate two arrays in Java?](http://stackoverflow.com/questions/80476/how-can-i-concatenate-two-arrays-in-java) – Julian Heinovski Mar 12 '17 at 16:40
  • @Julian that is not exactly what is being asked here. The point is that updating the underlying array should update the concatenated array. – Andy Turner Mar 12 '17 at 16:42
  • The two underlying arrays are already stored in memory, it would be trivial to "join" them by the ends, but would take much longer to copy them to a new memory location. – Bloc97 Mar 12 '17 at 16:43
  • Exactly how much is "much" longer? How did you measure it? What percentage of overall run time was the difference? – Lew Bloch Mar 12 '17 at 17:54
  • @LewBloch Copying an array containing 10 minutes of audio at 44100Hz takes on my computer 43ms to complete, it's pretty bad. Used System.nanoTime() to test. Considering the complete operation on the audio took 231ms to complete, I should be able to cut it down to 188ms if the concatenation at the end does not copy the entire array. – Bloc97 Mar 12 '17 at 19:07
  • A list of lists might be the way to go, then. Good job having the numbers; it helps folks understand the real problem. Also, separate the post-processing from the data gathering. Your requirement to be able to change data in the middle should not be time sensitive, only the raw data gathering. Maybe an inner list can hold some set number of frames, and the outer list can hold many consecutive sets of frames. – Lew Bloch Mar 12 '17 at 22:38

2 Answers2

5

No, this is not possible in Java.

Harald Gliebe
  • 7,236
  • 3
  • 33
  • 38
  • So is there some workaround possible? Copying millions of entries each time doesn't seem sound at all... – Bloc97 Mar 12 '17 at 16:41
  • 1
    Depends what problem you are trying to solve. You could allocate the concatenated array and have a class that defines a view on a range of the array. – Harald Gliebe Mar 12 '17 at 16:46
  • That would be difficult to generalise, especially if I keep concatenating multiple arrays. How would I store and access those arrays? Should I just use an arrayList of the arrays and use a getter to "fake" the indexes? – Bloc97 Mar 12 '17 at 16:48
  • You could use LinkedList, or write your own implementation. I don't see another way of doing it –  Mar 12 '17 at 16:56
  • What are you trying to "work around"? Why are you using arrays to hold "millions of entries" anyway? "Seem" is a subjective term; what is the objective reality you are dealing with? How did you identify the problem you're trying to solve? What is your evidence for the severity of the problem? – Lew Bloch Mar 12 '17 at 17:57
0

This is not possible in java. In java you can send by reference only objects, For example:

Obj o1= new Obj(1), o2 =..., o6 = new Obj(6);
Obj [] a= {o1, o2, o3};
Obj [] b= {o4, o5, o6};
Obj [] ab = SomeMergeFuncNoCopyConstractor(a, b); //{o1, o2, o3, o4, o5, o6}
o3.setValue(90);
Obj.printArray(a); //1,2,90
Obj.printArray(ab);//1,2,90,4,5,6

This can be more efficient in some cases, depending on use. But you will have to use objects.

Moshe9362
  • 352
  • 2
  • 15