coder
coder

Reputation: 13250

Add two array list values to arraylist c#

I'm completely new to c# sorry if asked here anything meaningless for you guys but I would like to know how can I solve this type of situation.

I having two arraylist's as shown below:

 ArrayList OldLinks = new ArrayList();
 ArrayList NewLinks = new ArrayList();
 ArrayList mylist   = new ArrayList();

 foreach (string oldlink in OldLinkArray)
     {
        OldLinks.Add(oldlink);
     }
 foreach (string newlink in NewLinkArray)
     {
         NewLinks.Add(newlink);
     }

Now I need to get them as single arraylist with two items each

I need to get it as

ArrayList NewList = new ArrayList();

NewList.Add(oldlink, newLink);

Upvotes: 1

Views: 6141

Answers (3)

Hari Prasad
Hari Prasad

Reputation: 16956

As you need both oldlink and newlink together as an item in resulted arraylist, you could use Zip Linq extension and do this.

ArrayList NewList = new ArrayList();
NewList.AddRange(OldLinks.Cast<string>()
                         .Zip(NewLink.Cast<string>(), (x,y) =>  string.Format("{0},{1}",x,y))
                         .ToArray()
                ); 

Result ArrayList contains both (oldlink, newlink).

Upvotes: 0

Pushpendra
Pushpendra

Reputation: 1712

ArrayList NewList = new ArrayList();
NewList.AddRange(OldLinks);
NewList.AddRange(NewLinks);

You can use AddRange() method or AddAll() method to accomlish this.

NewList.AddAll(OldLinks);
NewList.AddAll(NewLinks);

Or To create multidimensional arrayList you can use dictionary

 public class MultiDimList: Dictionary<string, string>  { }
 MultiDimList NewList = new MultiDimList ();
 for(int i; i<OldLinks.Count ; i++)
 {
   NewList.Add(OldLinks[i].ToString(), NewLinks[i].ToString()); 
 }

provided both ArrayLists have the same count

Upvotes: 3

Constantin Treiber
Constantin Treiber

Reputation: 420

Xou could do something like this.. The string Version is problaby not the best solution but can work. Sorry Code is note tested

public class Link
    {
    public string Version {get;set;}
    public string Value {get;set;}
    }

Use it Like

    List<Link> linkList = new List<Link>();
    linkList.AddRange(OldValues)
    linkList.AddRange(OldValues)

    var oldList = linkList.Where(l => l.Version.Equals("old")).ToList();
    var newList = linkList.Where(l => l.Version.Equals("new")).ToList()

Upvotes: 0

Related Questions