Menu
Topics Index
...
`

Merge the string arrays


If you need explanation Read this topic

If you need Answer Take test on this topic

Write a program to merge the string arrays.

Input (String Array1, String Array2) Output (String Array)
{1, 2, 3, 4, 5, 6, 7}
{Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}
{1, 2, 3, 4, 5, 6, 7, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday}
{A, B, C, D}
{Apple, Ball, Cat, Dog, Egg}
{A, B, C, D, Apple, Ball, Cat, Dog, Egg}
{}
{Apple, Ball, Cat, Dog, Egg}
{Apple, Ball, Cat, Dog, Egg}
{}
{}
{}
null
null
null


            
class MergeStringArrays
{
    public static void main(String s[])
    {
        String weekNumbers[] = {"1", "2", "3", "4", "5", "6", "7"};
        String weekNames[] =
        {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        String[] mergedStrings = mergeStringArrays(weekNumbers,weekNames);
        System.out.print("The merged strings are: ");
        for(String string : mergedStrings){
        System.out.print(string + ", ");
        }

    }


    public static String[] mergeStringArrays(String[] array1, String[] array2)
    {
        String[] result = null;
        return result;
    }

}
Topic: Unknown

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

check if the arrays are nul,if null than return null;
here we need to merge the given arrays..
* create an ArrayList with String
* now add the all elements of array by converting it into list
list.addAll(array1.asList());
list.addAll(array2.asList());
now convert this list to array by using the toArray() and type cast it into String
Case 2:
create the res array with the size array1.length+array2.length
*start for loop i=0;i<array1.length;i++)
res[index++]=array1[i];

now start another loop to copy the 2nd array elements
*for loop i=0;i<array2.length;i++)
res[index++]array[i];

Posted by Uday Kumar    2015-02-23 06:05:03


here we are given two list and we have to merge both the list intp one list...the size of output list will be equal to the sum of length of both input list....take loop a=0 to list1.length { result[a]=list1[a]} ....take p=0 .....take another loop..... b=list1.length to (list1.length   plus list2.length) { rsult[b]=list2[p] ...increment p by one} result will ne holding desired output

Posted by Asma Mujtaba Khan    2015-02-23 06:05:22


Here we have to concatinate two string arrays.

First we create result string array whose length will be sum of both the input arrays.

Int count=a.length+b.length
String result=new String[count];

After that we run for loop till count.Inside for loop we check if counter 'I' is equal to a.length-1 then we take another array element into result array.

For(int I=0;I<count;I++)
{
If(I==a.length-1)
Result[I]=b[I];
Else
Result[I]=a[I];
}
Here we are checking if we have reached the end of first array then we will take second array elements to result array.

After that we return the result array.Result will contain the concatination of both the arrays.

Posted by Mânïshå Mùlchåndânï    2015-02-23 16:34:35


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Asma Mujtaba Khan,  for 'Second Correct Comment'. The 'lucky liker' is Shashanka Mogaliraju. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-02-24 04:49:48


© meritcampus 2019

All Rights Reserved.

Open In App