How to copy an array to another array that already has data?

How to copy an array, say

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

to another array that already has data in it,

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

I want to add arraytobecopied to the end of a new array and store the values ​​in an array. just like a side note, it will be a process that will jump to the end of the array each time.

Should I just use a for loop? or is there a better way. (I can not use Array) already tried: (

+4
source share
5 answers

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

For example, here is an example 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 can use java.util.ArrayList , which automatically processes the extension of the internal array. Its toArray() methods make it easy to convert a list to an array when required.

+4
source

This question was asked here earlier, you can see this page for an answer. How can I combine two arrays in Java?

Use System.arraycopy

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

+10
source

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

If you need to use a simple array of primitives (rather than a wrapper class), you can use several methods in java.util.Arrays and java.lang.System 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 reassign a variable only to a new array (e.g. newarray = result; ).

+1
source

The easiest way:

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

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

How to write one method that updates the destination array with src array. Now that you have the copy elements, make sure that this size is sufficient in the target array. Otherwise, when creating a new array, create it with a double size and copy the elements, this can help to avoid creating a new array every time and repeat iterations to fill the full array after resizing.

It is very similar to how the list of arrays supports the size of the array inside it.

0
source

Source: https://habr.com/ru/post/1485545/


All Articles