简体   繁体   English

如何在Java中连接两个字符串数组

[英]How to concat two string arrays in Java

I am using JDK 1.7 and Eclipse and trying to concat two string arrays: 我正在使用JDK 1.7和Eclipse并尝试连接两个字符串数组:

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. 我正在使用Maven来构建项目。

It's not enough to import a type. import类型是不够的。 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. 就像你没有在类路径中提供包含上述类型的jar

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. 不像ArrayUtils.addAll()那么漂亮,但功能齐全。 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 下载common.codec-1.9.jar (下载zip并解压缩,你会找到jar文件)然后如果你正在使用像IDE这样的IDE

Eclipse: 日食:

1.Right-click your Project. 1.右键单击您的项目。

2.Select Properties. 2.选择属性。

3.On the left-hand side click java build path. 3.在左侧单击java构建路径。

4.Under Libraries Tab, click Add External Jars button. 4.在Libraries选项卡下,单击Add External Jars按钮。

5.Choose the downloaded file and click ok 5.选择下载的文件,然后单击“确定”

Netbeans : Netbeans:

1.Right-click your Project. 1.右键单击您的项目。

2.Select Properties. 2.选择属性。

3.On the left-hand side click Libraries. 3.在左侧单击“库”。

4.Under Compile tab - click Add Jar/Folder button. 4.在“编译”选项卡下 - 单击“添加Jar /文件夹”按钮。

Add right Maven dependency to your POM: 将正确的Maven依赖项添加到您的POM:

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

Why don't you do a for loop and store all the elements to one String and then use .split ? 为什么不进行for循环并将所有元素存储到一个String然后使用.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 ;) 我从纸上做了这个,但我几乎肯定它会起作用;)

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): 我发现避免arrayCopy()和copyOf()中所有startingIndex和endingIndex参数的最简单方法是编写一个特定的方法(这很简单):

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: 所以这里是concatTwoStringArrays()方法的用法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM