tags:

views:

209

answers:

5

Hi,

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?

A: 

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]);
}
Chris Arnold
-1. Index out of range if ar1.Length < ar2.Length!
John Buchanan
True. I was taking the question literally (exact array lengths).
Chris Arnold
I am getting this error when I tried thisIndex was out of range. Must be non-negative and less than the size of the collection.Parameter name: index and @John Buchanan and @chris I don't think so there is any property called ar1.Length? with ArrayList....
TSSS22
@ps123 try my answer now. it works...
John Buchanan
+1  A: 

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]);
    }
}
John Buchanan
Thanks John its working now .....Thanks a lot....
TSSS22
+1  A: 

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.

Mark Byers
A: 

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]);
}
jestro
A: 

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());

}

}

abdelatif
That's wrong. The extra space requirement by the threads is in the order of hundrets of kilobytes just for the stack. Additionally, you are modifying a `HashMap`, which is explicitly documented as non-threadsafe, from multiple threads at the same time. Third, if an `InterruptedException` occurs, you still print a result, and this result is most likely not complete. Fourth, you depend on the `hashCode` of an Integer being exactly its value and `HashMap` using this hashCode unmodified. The latter assumption doesn't hold. Therefore, you output the values in a wrong order.
Roland Illig
Fifth, creating a `Thread` took about one millisecond when I tested it, when the non-threaded code would only take some nanoseconds to execute.
Roland Illig