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,140 points
2,861 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 Parth
• 4,640 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,920 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 8, 2019 in Java by sampriti
• 1,120 points

edited Aug 8, 2019 by Kalgi 2,266 views
0 votes
0 answers

How to manage two JRadioButtons in java so that only one of them can be selected at a time?

How to manage two JRadioButtons in java ...READ MORE

Apr 21, 2020 in Java by kartik
• 37,520 points
681 views
0 votes
0 answers

how to compare two strings in java?

how to compare two strings in java? READ MORE

Nov 16, 2021 in Java by Naresh
• 210 points
614 views
0 votes
1 answer

How to merge two arrays in JavaScript and de-duplicate items?

With Underscore.js or Lo-Dash you can use: console.log(_.union([1, ...READ MORE

answered Feb 18, 2022 in Java by Aditya
• 7,680 points
511 views
+1 vote
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,620 points
1,326 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,220 points
3,576 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 10, 2018 in Java by samarth295
• 2,220 points
888 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,370 points
811 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 Parth
• 4,640 points
957 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,140 points
2,548 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP