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.
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.
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.
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.
addAll() method to concatenate two arrays into one. This method works for both primitive as well as generic type arrays.
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.
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);
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);
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.
Add right Maven dependency to your POM:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With