Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert array into another array

Tags:

java

arrays

Writing method to insert a[] into numbers[] at a position stored in variable "location".

    public boolean insertArray(int location, double a[])
    {    
        if (length != MAX_CAPACITY)
        {
            numbers[location] = a[];
            length++;
            return true;
        }
        return false;
    }

Is it possible to pass through an array?

like image 886
bap Avatar asked Oct 16 '25 11:10

bap


1 Answers

You can use System.arraycopy :

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

Here is a simple example you can follow to solve your problem :

double a[] = {1, 2, 3, 4, 5};
double b[] = {6, 7, 8};
int local = 5;
double result[] = new double[a.length + b.length];

System.arraycopy(a, 0, result, 0, a.length);
System.arraycopy(b, 0, result, local, b.length);
System.out.println(Arrays.toString(result));

Output

[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]
like image 86
YCF_L Avatar answered Oct 18 '25 02:10

YCF_L



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!