1

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);
5
  • 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<T>)! Commented May 25, 2016 at 6:51
  • @Kai-Thanks for pointing out..sure will have go through it :) Commented May 25, 2016 at 6:52
  • Don't you want to use a key value pair? Like a Dictionary? Commented May 25, 2016 at 6:52
  • @Nikhil-Can you show me the way how to use it Commented May 25, 2016 at 6: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 for speed difference, HashSet is faster Commented May 25, 2016 at 7:13

3 Answers 3

3
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

Sign up to request clarification or add additional context in comments.

2 Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.