Java 数据结构 - 合并两个数组
一种方法是,创建一个长度等于两个数组长度之和的数组,然后将两个数组的元素逐个添加到其中。
示例
import java.util.Arrays; public class JoiningTwoArrays { public static void main(String args[]) { String[] arr1 = {"JavaFX", "OpenNLP", "OpenCV", "Java"}; String[] arr2 = {"Hadoop", "Sqoop", "HBase", "Hive" }; String[] result = new String[arr1.length+arr2.length]; int count = 0; for(int i = 0; i<arr1.length; i++ ) { result[i] = arr1[i]; count++; } for(int i = 0; i<arr2.length; i++ ) { result[count++] = arr2[i]; } System.out.println("Contents of the resultant array ::"); System.out.println(Arrays.toString(result)); } }
输出
Contents of the resultant array :: [JavaFX, OpenNLP, OpenCV, Java, Hadoop, Sqoop, HBase, Hive]
广告