Java Tutorial
This java program is used to merge two arrays into a single array using addAll method if List class and Arrays.toString method of Array class.
import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class MergeArraysTest { public static void main(String[] args){ String numberList[] = {"1","2","3"}; String strList[] = {"C", "O", "D", "I", "N", "G"}; System.out.println("Merge Arrays"); System.out.println("-------------"); System.out.println("Array 1: " + Arrays.toString(numberList)); System.out.println("Array 2: " + Arrays.toString(strList)); List list = new ArrayList(Arrays.asList(numberList)); list.addAll(Arrays.asList(strList)); Object[] collection = list.toArray(); System.out.println("Merged Array: "+Arrays.toString(collection)); } }Output:
D:\Java_Programs>javac MergeArraysTest.java D:\Java_Programs>java MergeArraysTest Merge Arrays ------------- Array 1: [1, 2, 3] Array 2: [C, O, D, I, N, G] Merged Array: [1, 2, 3, C, O, D, I, N, G]
This java program is used to merge multiple arrays into a single array using addAll method if List class and Arrays.toString method of Array class.
import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class MergeArraysTest { public static void main(String[] args){ String numberList[] = {"1","2","3"}; String strList1[] = {"C", "O", "D", "I", "N", "G"}; String strList2[] = {"P", "O", "I", "N", "T", "E", "R"}; System.out.println("Merge Arrays"); System.out.println("-------------"); System.out.println("Array 1: " + Arrays.toString(numberList)); System.out.println("Array 2: " + Arrays.toString(strList1)); System.out.println("Array 3: " + Arrays.toString(strList2)); List list = new ArrayList(Arrays.asList(numberList)); list.addAll(Arrays.asList(strList1)); list.addAll(Arrays.asList(strList2)); Object[] collection = list.toArray(); System.out.println("Merged Array: "+Arrays.toString(collection)); } }Output:
D:\Java_Programs>javac MergeArraysTest.java D:\Java_Programs>java MergeArraysTest Merge Arrays ------------- Array 1: [1, 2, 3] Array 2: [C, O, D, I, N, G] Array 3: [P, O, I, N, T, E, R] Merged Array: [1, 2, 3, C, O, D, I, N, G, P, O, I, N, T, E, R]
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page