Dakota Miller
Dakota Miller

Reputation: 493

How to copy a array into another array that already has data in it?

How would I copy an array say

float arraytobecopied[] = {1.20,2.50,3.60};

to another array that has data in it already say

float newarray[] = {5.20,6.30,4.20};

I want to add the the arraytobecopied to the end of the new array and keep the values in the array. also as a side note this would be an on going process adding to the end of the array every time.

Should i just use a for loop? or is there a better way. (Can't use Array) already tried:(

Upvotes: 6

Views: 46445

Answers (5)

ajay.patel
ajay.patel

Reputation: 1967

If you dont want to use anything from java.Util at all.

How about writing one method that updates the destination array with src array. Now when u copy elements make sure that the size is enough in destination array. Otherwise when you create a new array, create it with double size and copy the elements, this may help in avoiding a new array creation everytime and doing iterations to populate the complete array again after resizing.

Somewhat similar to how array list maintains size of array inside it.

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234795

The easiest approach from a programming perspective is to use a List<Float> (if you can use Float values instead of float) or a third-party library such as Apache Commons Collections or Trove that provides dynamic arrays of primitives.

If you need to use a simple array of primitives (and not a wrapper class), you can use a couple of methods in the java.util.Arrays and java.lang.System classes to help:

int len1 = newarray.length;
int len2 = arraytobecopied.length;
float[] result = Arrays.copyOf(newarray, len1 + len2);
System.arraycopy(arraytobecopied, 0, result, len1, len2);
// result now contains the concatenation of newarray and arraytobecopied

Note that you cannot change the length of an array object; you can only reassign the variable to a new array (e.g., newarray = result;).

Upvotes: 3

Marcelo Dias
Marcelo Dias

Reputation: 429

The easiest can be:

List<Float> floats = new ArrayList(arraytobecopied);
floats.addAll(newarray);
arraytobecopied = floats.toArray(new float[0]);

Upvotes: 1

Andy Thomas
Andy Thomas

Reputation: 86381

You can't increase the size of the original array. But you could create a new array, copy both source arrays into it, and assign your reference variable to it.

For example, here's a sketch of a simple implementation. (An alternative is to use System.arraycopy().)

 float[] newerArray = new float[ newarray.length + arraytobecopied.length ];
 for ( int i = 0; i < newarray.length; ++i ) {
     newerArray[i] = newarray[i];
 }
 for ( int i = 0; i < arraytobecopied.length; ++i ) {
     newerArray[ newarray.length + i ] = arraytobecopied[i];
 }
 newarray = newerArray; // Point the reference at the new array

Alternatively, you could use a java.util.ArrayList, which automatically handles growing the internal array. Its toArray() methods make it easy to convert the list to an array when required.

Upvotes: 5

DevZer0
DevZer0

Reputation: 13525

This question has been asked here before, You can see this page for the answer. How can I concatenate two arrays in Java?

Use System.arraycopy

public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

Upvotes: 14

Related Questions