How to concatenate two arrays in Java?

0 votes
I was working on two different arrays. But, now I want to concatenate these two arrays. How to do it?
Apr 24, 2018 in Java by Daisy
• 8,050 points
402 views

2 answers to this question.

0 votes

To concatenate two arrays, we can use Apache library.

import org.apache.commons.lang.ArrayUtils;
public class ConcatenateArray {

    public static void main(String args[]) { 

        int [] first = {1,2,3, 4};

        int [] second = {5,6,7,8};

      

        // combine two arrays in Java using Apache commons ArrayUtils

        int [] combined = ArrayUtils.addAll(first, second);

     

        System.out.println("Concatenated array : " + Arrays.toString(combined));

      }
}

      

       

answered Apr 24, 2018 by parth295
• 4,600 points
0 votes
public <T> T[] concatenate(T[] a, T[] b) {
    int aLen = a.length;
    int bLen = b.length;

    @SuppressWarnings("unchecked")
    T[] c = (T[]) Array.newInstance(a.getClass().getComponentType(), aLen + bLen);
    System.arraycopy(a, 0, c, 0, aLen);
    System.arraycopy(b, 0, c, aLen, bLen);

    return c;
}

It will not work with primitive data type, it will work only with object type.

answered Jul 19, 2018 by Sushmita
• 6,870 points

Related Questions In Java

0 votes
1 answer

how to compare two words in java?

hey,  I think in your code the function ...READ MORE

answered Aug 7 in Java by sampriti
• 1,100 points

edited Aug 8 by Kalgi 49 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,380 points

edited Nov 6, 2018 by Omkar 7,276 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered 4 days ago in Java by Sirajul
• 19,670 points
112 views
+1 vote
2 answers

How to generate random integers within specific range in Java?

You can achieve that concisely in Java: Random ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
43 views
0 votes
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,540 points
52 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
267 views
0 votes
2 answers

How to convert array into list in Java?

Another workaround if you use apache commons-lang: int[] ...READ MORE

answered Aug 9, 2018 in Java by samarth295
• 2,190 points
43 views
0 votes
2 answers

Array of Objects

You can also do : A[] a = ...READ MORE

answered Aug 3, 2018 in Java by sharth
• 3,320 points
32 views
0 votes
1 answer

How to calculate the difference between two date instances in Java?

You can use Joda Time Library. Interval i ...READ MORE

answered May 4, 2018 in Java by parth295
• 4,600 points
51 views
0 votes
5 answers

How to compare Strings in Java?

String fooString1 = new String("foo"); String fooString2 = ...READ MORE

answered Jul 12, 2018 in Java by Daisy
• 8,050 points
106 views