Combine/concatenate two arrays of integers

area 51

I'm new to Java, and I know there are way better ways to concatenate two arrays like using IntStream.concat method, but as I want to learn, I just preferred to do it in this way

I'm trying to concatenate two arrays of integers using below function

public static int[] mergeAndOrder(int[] array1, int[] array2) {

    int [] tmp= new int [array1.length+ array2.length];
    int k=array1.length;
    for(int i=0; i<array1.length; i++)
    {
        tmp[i]=array1[i];
    }

    for(int i=0; i< array2.length; i++)
    {
        tmp[k]=array2[i];
        k++;
    }
    return tmp;
}

is it correct?

it seems it did not concatenate two arrays of integers

Heath Morgan

I think your approach should work. I wrote a simple test to prove it:

@Test
public void test()
{
    int[] a = {1, 2, 3};
    int[] b = {2, 3, 4};

    int[] expected = {1, 2, 3, 2, 3, 4};
    int[] actual = mergeAndOrder(a, b);

    log.info(Arrays.toString(a));
    log.info(Arrays.toString(b));
    log.info(Arrays.toString(actual));

    assertThat(actual, is(expected));
}

My log statements seem to agree:

2023-06-19 13:48:34,953 INFO  [] (main) [1, 2, 3]
2023-06-19 13:48:34,968 INFO  [] (main) [2, 3, 4]
2023-06-19 13:48:34,968 INFO  [] (main) [1, 2, 3, 2, 3, 4]

Like user16320675, I am wondering why you think it did not concatenate the two arrays; it appears to work.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to add two integers represented by two arrays efficiently, putting the solution into another array

Power Of Two Integers

mid value of two integers

[InterviewBit]Power Of Two Integers

How to determine if integers are within a range and store into two arrays?

Reading two lines of integers

Same sign of two integers

Is there a way to compare the corresponding integers in two numbers without converting them to arrays

Add two arrays of integers into an array of arrays so it creates coordinates pairs

How to combine two arrays of integers and return average?

Numpy: Conserving sum in average over two arrays of integers

Sorting an array of arrays of integers

Check common elements of two-dimensional arrays of integers, considering the positions they are on

Mapping function for two integers

Two, one dimensional arrays of integers that returns true

comparing two arrays of integers in c acts odd

Returning two integers with loops?

java, two integers, they are equal but

sorting positive and negative integers through two integer arrays

Comparing two arrays of integers and returning the subscript (C)

Splitting string into two integers

Best way to find and match two integers from different arrays in swift 3? (Multidimensional Array)

How to subtract arrays of integers

Bug in methods that find unmatched integers between two arrays

Balance two arrays of integers evenly

How can I find duplicate integers between two arrays and copy them into a new array?

Given two arrays A & B with positive integers, find Maximum product of Array A after atmost N operation

How to concat corresponding elements (which are integers) of two 2D arrays of the same shape?

given two arrays A and B of N integers each, describing a directed graph, returns true if the graph is a cycle and false otherwise