6

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.

3
  • 1
    You want to make a bigger String[] containing the elements of the two other arrays? Commented Apr 24, 2014 at 17:14
  • 1
    use ArrayList as typr for both if you dont mind Commented Apr 24, 2014 at 17:22
  • had to use <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency> to resolve this use user3567218's suggestion below if you are not using maven Commented Apr 25, 2014 at 19:37

7 Answers 7

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

had to use <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.9</version> </dependency> to resolve this . Then used String[] parts = ObjectArrays.concat(a1,a2,String.class); Response from "user3567218" is another way when you are not using maven.
4

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);

Comments

3

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);

1 Comment

I don't think you need that +1 .
1

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.

Comments

1

Add right Maven dependency to your POM:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.3.2</version>
</dependency>

Comments

0

Why don't you do a for loop and store all the elements to one String and then use .split ?

String result = null;
for(String aux : a1){
   final += aux + ";";
}

for(String aux1 : a2){
   final += aux1 + ";";
}
String[] finalArray= result.split(";");

I made this out of the paper, but I'm almost sure it will work ;)

4 Comments

That's going to add a lot of short-lived objects to the heap that won't be garbage collected until the loop finishes. If the arrays are large enough, you'll run out of memory.
Yes, I know. But for the question it's a simple simple solution that can help on future projects ;)
I didn't notice that you named the String variable "final" before. That will never work; it's a reserved keyword. It's a simple solution, sure, but it's not memory efficient. The best solutions are either to use charAt() as others have written or to use toCharArray(). This will be the fastest and will use the least memory. Your solution will tend to produce OutOfMemoryErrors quite quickly if you're dealing with long Strings or in a multi-threaded environment (as on a server).
I used a Portuguese word lol, I meant more like result. I know it's not efficient on a long term, but for the question it was a quick solution. And I made this during my job, so I didn't thought very much on a solution, it was the first that came to my mind ;)
0

I found that the easiest way to avoid all those startingIndex and endingIndex parameters in arrayCopy() and copyOf() is to write a specific method (which is straightforward):

public static String[] concatTwoStringArrays(String[] s1, String[] s2){
    String[] result = new String[s1.length+s2.length];
    int i;
    for (i=0; i<s1.length; i++)
        result[i] = s1[i];
    int tempIndex =s1.length; 
    for (i=0; i<s2.length; i++)
        result[tempIndex+i] = s2[i];
    return result;
}//concatTwoStringArrays().

So here's the usage of concatTwoStringArrays() method:

String[] s3 = concatTwoStringArrays(s1, s2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.