Java中如何把两个数组合并为一个

2024-11-23 05:44:44
推荐回答(2个)
回答1:

package 数组与数组合并;
import java.util.Arrays;

//Java中如何把两个数组合并为一个
public class gog {
public static void main(String[] args) {
String [] str1 = {"J","a","v","a","中"};
String [] str2 = {"如","何","把","两","个","数","组","合","并","为","一","个"};

int strLen1=str1.length;//保存第一个数组长度
int strLen2=str2.length;//保存第二个数组长度
str1= Arrays.copyOf(str1,strLen1+ strLen2);//扩容
System.arraycopy(str2, 0, str1, strLen1,strLen2 );//将第二个数组与第一个数组合并
System.out.println(Arrays.toString(str1));//输出数组

}
}

回答2:

可以先放入集合中 然后再转成数组