Write a method that merges two array lists, altern

2020-01-29 10:11发布

Write a method

public static ArrayList merge(ArrayList a, ArrayList b)

that merges two array lists, alternating elements from both array lists. If one array list is shorter than the other, then alternate as long as you can and then append the remaining elemts from the longer array list. For example, if a is

1 4 9 16

and b is

9 7 4 9 11

then merge returns the array list

1 9 4 7 9 4 16 9 11


What I tried doing was writing a for loop with if statements such that a number is added to the merge array list from array list a when i is an even number (i%2==0) and from array list b when i is an odd number. I am however not sure how to deal with the fact that one array list can be longer than the other. Could anyone please help me out?

EDIT: Ok, here is the code (but it is far from correct):

public static ArrayList<Integer> merge(ArrayList<Integer> een, ArrayList<Integer> twee)
{
    ArrayList<Integer> merged = new ArrayList<Integer>();

    for(int i = 0; i<100; i++)
    {           
        if(i%2!=0)
        {
            merged.add(a.get(i));
        }   
        if(i%2 == 0)
        {
            merged.add(b.get(i));
        }               
    }

    System.out.println(merged);
    return merged;
}

标签: java
10条回答
姐就是有狂的资本
2楼-- · 2020-01-29 10:36

Try this

Iterator iterator1 = arr1.iterator();
     Iterator iterator2 = arr2.iterator();
     while (iterator1.hasNext() || iterator2.hasNext()) {
       if(iterator1.hasNext()){
         mergerArr.add(iterator1.next());
       }
       if(iterator2.hasNext()){
         mergerArr.add(iterator2.next());
       }
     }
查看更多
ら.Afraid
3楼-- · 2020-01-29 10:43

Array1= {1,2,3} Array2= {a,b,c,d,e}

Output= {1, a, 2, b, 3, c, d, e}

public class MergeArray {

public static void main(String args[])
{
char [] arr1= {'1','2','3'};
char [] arr2= {'a','b','c','d','e'};


int l1= arr1.length;
int l2=arr2.length;

int l3=l1+l2;

char [] arr3=new char[l1+l2];

int i=0;
int j=0;
int k=0;
int m=0;
int r=0;

if(l1<l2)
    r=l1;
else
    r=l2;

while(m<r)
{
    arr3[k++]=arr1[i++];
    arr3[k++]=arr2[j++];
    m++;
}

while(k<l3)
{
    if(l1<l2)
        arr3[k++]=arr2[j++];
    else
        arr3[k++]=arr1[i++];
}

for(int n=0;n<l3;n++)
{
    System.out.print(arr3[n]+" ");
}

}

}

查看更多
迷人小祖宗
4楼-- · 2020-01-29 10:44

Without Iterator:

public static ArrayList merge(ArrayList a, ArrayList b) {
    int c1 = 0, c2 = 0;
    ArrayList<Integer> res = new ArrayList<Integer>();

    while(c1 < a.size() || c2 < b.size()) {
        if(c1 < a.size())
            res.add((Integer) a.get(c1++));
        if(c2 < b.size())
            res.add((Integer) b.get(c2++));
    }
    return res;
}
查看更多
够拽才男人
5楼-- · 2020-01-29 10:44

Try this:I implemented using Array.

public static void main(String[] args) {
    int[] first = { 1, 4, 9, 16 };
    int[] second = { 9, 7, 4, 9, 11 };
    int[] merge = new int[first.length + second.length];
    int j = 0, k = 0, l = 0;
    int max = Math.max(first.length, second.length);
    for (int i = 0; i < max; i++) {
        if (j < first.length)
            merge[l++] = first[j++];
        if (k < second.length)
            merge[l++] = second[k++];
    }
    System.out.println(Arrays.toString(merge));
}

Output:

[1, 9, 4, 7, 9, 4, 16, 9, 11]
查看更多
何必那么认真
6楼-- · 2020-01-29 10:45

OK, my suggestion is to use ArrayList:

public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] list1 = in.nextLine().split(",");
        String[] list2 = in.nextLine().split(",");
        ArrayList<String> merged = new ArrayList<>();
        for (int i = 0; i < Math.max(list1.length,list2.length); i++) {
                merged.add(list1[i]);
                merged.add(list2[i]);
        }
        System.out.println(String.join(",", merged));
    }

You can change the code to Integer if you are sure that the input will be only numbers.

查看更多
【Aperson】
7楼-- · 2020-01-29 10:46

Here is my solution

LinkedList<Integer> list3 = new LinkedList<Integer>();


Iterator<Integer> itA = list.iterator();
Iterator<Integer> itB = list2.iterator();

while(itA.hasNext() && itB.hasNext()){
    list3.add(itA.next());
    list3.add(itB.next());
}
查看更多
登录 后发表回答