user219725
user219725

Reputation:

issue in adding two arraylist

I have two arrayList

ArrayList ar1 = new ArrayList();
ArrayList ar2 = new ArrayList();

ar1 is having values { A,B,C,D} and ar2 is having values {1,2,3,4}

I want to add these two array list such that output should be

A 1 B 2 C 3 D 4

I have used ar1.addrange(ar2); and the output is like this A B C D 1 2 3 4

Any suggestions how to resolve this?

Upvotes: 2

Views: 2146

Answers (6)

Rais Alam
Rais Alam

Reputation: 7016

Try below code it should work fine:

public static void main(String[] args) throws ParseException
{
    List<String> arr1 = new ArrayList<String>();
    arr1.add("1");
    arr1.add("2");
    arr1.add("3");
    arr1.add("4");

    List<String> arr2 = new ArrayList<String>();
    arr2.add("A");
    arr2.add("B");
    arr2.add("C");
    arr2.add("D");

    List<String> arr3 = mergeList(arr1, arr2);

    for (String string : arr3)
    {
        System.out.println(string);
    }

}

public static List<String> mergeList(List<String> arr1, List<String> arr2)
{

    int i = 0;
    int size = arr2.size();

    for (String string : arr1)
    {
        if (size >= (i + 1))
        {
            arr2.add(i + 1, string);
            i = i + 2;
        }
        else
        {
            arr2.add(string);
        }
    }

    return arr2;

}

Upvotes: 0

guest
guest

Reputation: 121

A Parallelized solution that will produce better time performance (approximately double) with no extra space requirement, in case of larger number of elements:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public class Main {


public static void main(String[] args) {

final ArrayList ar1 = new ArrayList();
ar1.add("A");
ar1.add("B");
ar1.add("C");
ar1.add("D");
final ArrayList ar2 = new ArrayList();
ar2.add("1");
ar2.add("2");
ar2.add("3");
ar2.add("4");

//Create a hashmap with size and loadfactor that prevent it
//from being expanded before filling it.
final HashMap ar3 = new HashMap(10, 0.9f);


//fill half the list by a thread
Thread thread1 = new Thread(){
    @Override
    public void run() {

    int i = 0;
    for (Iterator it = ar1.iterator(); it.hasNext();) {
        ar3.put(i, it.next()) ;
        i+=2;
    }
    }
};

//fill half the list by another thread
Thread thread2 = new Thread(){
    @Override
    public void run() {

    int j = 1;
    for (Iterator it = ar2.iterator(); it.hasNext();) {
        ar3.put(j, it.next()) ;
        j+=2;
    }
    }
};

//start threads
thread1.start();
thread2.start();

//Let main waits for them
try {
    thread1.join();
    thread2.join();
} catch (InterruptedException ex) {
    System.out.println(ex);
}

//print result
System.out.println(ar3.values());

}

}

Upvotes: 1

jestro
jestro

Reputation: 2593

Use a a List so that you avoid boxing! (Assuming the arrays are equal length)

List<Int32> fun = new List<Int32>();
for (Int32 i = 0; i < ar1.Length; ++i)
{
  fun.Add(ar1[i]);
  fun.Add(ar2[i]);
}

Upvotes: 1

John Buchanan
John Buchanan

Reputation: 5232

This should do the trick.


int max = Math.Max(ar1.Count, ar2.Count);
ArrayList ar3 = new ArrayList();

for (int i=0; i < max; i++)
{
    if (i < ar1.Count)
    {
       ar3.Add(ar1[i]);
    }
    if (i < ar2.Count)
    {
       ar3.Add(ar2[i]);
    }
}

Upvotes: 2

Mark Byers
Mark Byers

Reputation: 838176

The word you are looking for to describe that operation is 'zip'.

MoreLinq implements zip for Linq to Objects, so if you can use a newer version of .NET with Linq, you can just use that.

Upvotes: 2

Chris Arnold
Chris Arnold

Reputation: 5753

Try creating a new ArrayList to hold the solution...

ArrayList ar3 = new ArrayList();

for (int i = 0; i < ar1.Length; i++)
{
  ar3.Add(ar1[i]);
  ar3.Add(ar2[i]);
}

Upvotes: 1

Related Questions