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,090 points
1,246 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,630 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,890 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, 2019 in Java by sampriti
• 1,100 points

edited Aug 8, 2019 by Kalgi 1,443 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 in Java by kartik
• 20,090 points
154 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,420 points

edited Nov 6, 2018 by Omkar 26,630 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 Aug 20, 2019 in Java by Sirajul
• 55,340 points
445 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,580 points
154 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
1,684 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
101 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
107 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,630 points
140 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,090 points
252 views