0

I was wondering if it was possible to combine arrays, in a way in which the n+1 array values are increased by the nth array length.

I can do this simply using loops but i was wondering if it was possible other ways.

Array 1 = 1,2,3,4,5,6

Array 2 = 4,3,2,1

Combined array = 1,2,3,4,5,6 10,9,8,7

for example

float[] finalArray = new float[*length of combined arrays*];
int previousLength = 0;
for (float[] array : arrayList){
    for (int i = 0; i < array.length; i ++){
        finalArray[i + previousLength] = array[i] + previousLength;
        }
    previousLength += array.length
}

return finalArray

I'm hoping this explains what I'm trying to do.

I'm currently trying to optimise and it seems this function is taking up a lot of resources. I'm curious if there is a possible alternative.

Purpose:

I'm trying to scan through a dataset, do a complex function and return the results as a float array. Each value in the array would normally be offset by it's own index (to be used later). I am trying to seperate the dataset into smaller datasets and pass each slice to a thread. Then combine the results at the end.

  • 1
    If you are asking about different ways to program it, instead of having a problem, I would suggest you go over to http://codereview.stackexchange.com/ and post it there. – MortenMoulder Jan 13 '16 at 10:43
  • 2
    See http://stackoverflow.com/questions/80476/how-can-i-concatenate-two-arrays-in-java – Adisesha Jan 13 '16 at 10:44
  • How long are your arrays? Do you have any benchmarks? – vojta Jan 13 '16 at 10:45
  • @Snorlax Cheers did not know that was a place, I'll have a peek. –  Jan 13 '16 at 10:58
  • @vojta The arrays are dynamically sized and potentially infinite depending on the dataset size. I'm trying to scan through a dataset, do a complex function and return the results as a float array. Each value in the array would normally be offset by it's own index (to be used later). I am trying to seperate the dataset into smaller datasets and pass each slice to a thread. Then combine the results at the end. If this helps. –  Jan 13 '16 at 10:58
  • What version of Java are you using? You may benefit from new features in Java 8 – tucuxi Jan 13 '16 at 13:02
  • Most up to date version, so 8u65 I believe. –  Jan 13 '16 at 14:28

0 Answers0