Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have 3 arraylist I want to store data in 3rd arraylist which is same in arraylist1 and arraylist2.
how can I store data in arraylist3?
Posted
Updated 12-Sep-11 21:22pm
Comments
Herman<T>.Instance 13-Sep-11 2:57am    
show your code, so we can see sharp how to help you!
lukeer 13-Sep-11 3:14am    
ROTFL

C#
ArrayList ar1= new ArrayList();
ar1.add("1");
ArrayList ar2= new ArrayList();
ar2= (ArrayList)ar1.Clone();
 
Share this answer
 
v2
Comments
Pravin Patil, Mumbai 13-Sep-11 3:20am    
I think OP wants the common of arr1 and arr2 in arr3.
C#
ArrayList a1 = new ArrayList();
a1.Add( "1" );
ArrayList a2 = new ArrayList();
a2.Add( "2" );
ArrayList a3 = new ArrayList();
a3 = a2; //or a3 = a1; where-ever you want to copy data from.
 
Share this answer
 
Comments
Dalek Dave 13-Sep-11 4:07am    
Seems Reasonable.
sonu0009 5-Nov-11 5:27am    
i want to create id and name enter by user and search any name by id. in this program we use classlib and win app form
Hi,

Build arrays A & B, add values in array C[^] this will be helpful to you.
 
Share this answer
 
clone() method and manual way of adding two ArrayList is already mentioned above.
Here is the another way of merging two ArrayList into 3rd ArrayList. There is a method addAll which can be used for adding all the elements of a list to another list.

Lets say list is of String type. This is the way you should do it:
ArrayList<string> al1= new ArrayList<string>();
al1.add("1");
al1.add("2");
al1.add("3");
ArrayList<string> al2= new ArrayList<string>();
al2.add("1st");
al2.add("2nd");
al2.add("3rd");

//Final ArrayList
ArrayList<string> al3= new ArrayList<string>();
//Adding elements of arraylist1 to arraylist3
al3.addAll(al1);
//Adding elements of arraylist2 to arraylist3
al3.addAll(al2);


Source:
http://beginnersbook.com/2013/12/how-to-joincombine-two-arraylists-in-java/[^]

http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#addAll(java.util.Collection)[^]
 
Share this answer
 
v2
You can do it with Linq:

C#
List<string> list1 = new List<string>();
list1.Add("HHello");
list1.Add("Hello");
list1.Add("HelloO");
List<string> list2 = new List<string>();
list2.Add("HHello");
list2.Add("HelloX");
list2.Add("HelloO");
var list3 = from s in list1
        join t in list2
                    on s equals t
                    select s;
foreach (var v in list3)
        {
    Console.WriteLine(v);
        }
 
Share this answer
 
It might be helpful,

C#
class Program
{
    static void Main(string[] args)
    {

        List<string> listOne = new List<string>() { "listTwo_1", "listTwo_1", "listOne_3", "listTwo_1" };
        List<string> listTwo = new List<string>() { "listTwo_1", "listTwo_2", "listTwo_3", "listTwo_1" };
        Common(listOne, listTwo).ForEach(item => Console.WriteLine(item));
    }

    static List<string> Common(List<string> first, List<string> second)
    {
        return first.Where(item => second.Contains(item)).Distinct().ToList<string>();
    }
}


:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900