How to add two array in java.

How to add two array in Java.


Step 1- Create two array


String a0[] = { "A", "E", "I" };

String b0[] = { "O", "U" };


Step 2- Convert these arrays into a List


List a1 = new ArrayList(Arrays.asList(a0));

List b1 = new ArrayList(Arrays.asList(b0));



Step 3- Add the second List into the first List into the resultant third List



a1.addAll(b1);


Step 4- Convert the third list into Array.



Object[] c = a1.toArray();




AddTwoArray.java


package com.atozjavatutorials;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

public class AddTwoArray {

    public static void main(String arg[]){         

        String a0[] = { "A", "E", "I" };

        String b0[] = { "O", "U" };

        List a1 = new ArrayList(Arrays.asList(a0));

        List b1 = new ArrayList(Arrays.asList(b0));

        a1.addAll(b1);

        Object[] c = a1.toArray();

        //System.out.println(a1);

        for (int i = 0; i < c.length; i++) {

            System.out.println(c[i]);

        }

    }

}



Output:

A
E
I
O
U

Comments :

Post a Comment