0

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);
coder
  • 13,002
  • 31
  • 112
  • 214
  • 2
    if you're new to C# I just wanna point out that you never ever ever EVER use `ArrayList` unless you're forced at gunpoint by your boss. it's an ancient relic from a barbaric time when we didn't have generics. take a look at the more sensible `System.Collections.Generic` namespace (hint: `List`)! – sara May 25 '16 at 06:51
  • @Kai-Thanks for pointing out..sure will have go through it :) – coder May 25 '16 at 06:52
  • Don't you want to use a key value pair? Like a `Dictionary`? – Nikhil Girraj May 25 '16 at 06:52
  • @Nikhil-Can you show me the way how to use it – coder May 25 '16 at 06:53
  • @coder ah yes, best is use `Dictionary` or `HashSet` (if you do not have duplicate key-value at all), or `KeyValuePair`. `ArrayList` is rather outdated (though in some cases it *might* still be used), `List` is the way to go for collection of single type. `Dictionary` or `HashSet` for key-value collections. Check [this](http://stackoverflow.com/questions/37338620/which-one-is-faster-regex-or-endswith/37338859#37338859) for speed difference, `HashSet` is faster – Ian May 25 '16 at 07:13

3 Answers3

2
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

Pushpendra
  • 1,694
  • 14
  • 27
  • @Pushpendra- Thanks for your answer but how do I get the old links and new links from that array as they are all in one arraylist..I would like to get them as a,b – coder May 25 '16 at 06:51
  • I have updated the code for you. ArrayList just can't hold two data like multidimenisonal array does. What you can probably do is inherit dictionary create your List using it or you can just the combine the values of both arrayList prior to adding it to your new list – Pushpendra May 25 '16 at 07:05
0

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()
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Constantin Treiber
  • 420
  • 1
  • 7
  • 18
0

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).

Hari Prasad
  • 16,716
  • 4
  • 21
  • 35