6

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:(

Dakota Miller
  • 493
  • 1
  • 4
  • 22
  • 5
    The answer to your question on how to join the two arrays is most definitely using ApacheCommons `ArrayUtils.addAll`, but since you asked is there a better way. I would recommend using `ArrayList` as you can just keep appending values to it when necessary. – Kyle Jun 11 '13 at 04:21
  • Sweet thanks for the info but i cant use Array ive tried it already and it doesnt work for me needs. – Dakota Miller Jun 11 '13 at 04:25

5 Answers5

12

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)

Community
  • 1
  • 1
DevZer0
  • 13,433
  • 7
  • 27
  • 51
5

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.

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
3

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;).

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
1

The easiest can be:

List<Float> floats = new ArrayList(arraytobecopied);
floats.addAll(newarray);
arraytobecopied = floats.toArray(new float[0]);
Marcelo Dias
  • 409
  • 2
  • 18
0

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.

ajay.patel
  • 1,957
  • 12
  • 15