Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to concat two string arrays in Java

I am using JDK 1.7 and Eclipse and trying to concat two string arrays:

String [] a1 = { "a12", "b12" };
String [] a2 = { "c12", "d23", "ewe", "fdfsd" };

I have tried

String[] both = ObjectArrays.concat(a1,a2,String.class); 

imported

import com.google.common.collect.ObjectArrays;

getting Error:

can not resolve "import com.google.common.collect.ObjectArrays"

Can anyone help? I am using Maven to build the project.

like image 611
VictorGram Avatar asked Apr 24 '14 17:04

VictorGram


People also ask

How do you concatenate a string array in Java?

Using StringBuffer Create an empty String Buffer object. Traverse through the elements of the String array using loop. In the loop, append each element of the array to the StringBuffer object using the append() method. Finally convert the StringBuffer object to string using the toString() method.

How do I combine two string arrays?

In order to combine (concatenate) two arrays, we find its length stored in aLen and bLen respectively. Then, we create a new integer array result with length aLen + bLen . Now, in order to combine both, we copy each element in both arrays to result by using arraycopy() function.

Can we concatenate two arrays in Java?

Java doesn't offer an array concatenation method, but it provides two array copy methods: System. arraycopy() and Arrays. copyOf(). We can solve the problem using Java's array copy methods.

What is the method used if you want to concatenate two arrays?

addAll() method to concatenate two arrays into one. This method works for both primitive as well as generic type arrays.


5 Answers

It's not enough to import a type. You need to actually provide that type on the classpath when compiling your code.

It seems

can not resolve "import org.apache.commons.lang3.ArrayUtil"

like you haven't provided the jar containing the type above on your classpath.

like image 159
Sotirios Delimanolis Avatar answered Sep 29 '22 23:09

Sotirios Delimanolis


Alternatevely you can do it this way

    String[] a3 = Arrays.copyOf(a1, a1.length + a2.length);
    System.arraycopy(a2, 0, a3, a1.length, a2.length);
like image 37
Evgeniy Dorofeev Avatar answered Sep 29 '22 21:09

Evgeniy Dorofeev


This code should work. Not as pretty as the ArrayUtils.addAll(), but functional. You also can avoid having to import anything and you won't need to ship a 3rd party library for just one function.

String[] both = new String[a1.length + a2.length];
System.arraycopy(a1,0,both,0, a1.length);
System.arraycopy(a2,0,both,a1.length + 1, a2.length);
like image 33
mttdbrd Avatar answered Sep 29 '22 22:09

mttdbrd


Download common.codec-1.9.jar (Download zip and extract you will find the jar file) then if you are using an IDE like

Eclipse:

1.Right-click your Project.

2.Select Properties.

3.On the left-hand side click java build path.

4.Under Libraries Tab, click Add External Jars button.

5.Choose the downloaded file and click ok

Netbeans :

1.Right-click your Project.

2.Select Properties.

3.On the left-hand side click Libraries.

4.Under Compile tab - click Add Jar/Folder button.

like image 28
user3567218 Avatar answered Sep 29 '22 23:09

user3567218


Add right Maven dependency to your POM:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.3.2</version>
</dependency>
like image 40
Denis Kulagin Avatar answered Sep 29 '22 23:09

Denis Kulagin