0

Hello I was wonder if there was any ways I could merge multiple arraylist into a multi array.

Exmaple

I have arraylist1 which holds names. arraylist 2 which holds phone number and arraylist 3 which holds address. how could i merge these into one multi array so it would be as a record?

2
  • 3
    What version of .NET (helps me know whether you could utilize LINQ or not, also whether you have access to List<T> instead of ArrayList)? Commented Jun 1, 2011 at 18:41
  • 2
    The the arrays in the same order? What I mean is, is the Names[0] = PhoneNumbers[0] = Addresses[0]? Commented Jun 1, 2011 at 18:41

3 Answers 3

2

Lets suppose you have three ArrayList of string and all elements at ith index forms a record,

ArrayList Names = new ArrayList();
ArrayList Phone = new ArrayList();
ArrayList Address = new ArrayList();
ArrayList res = new ArrayList();

for(int i=0; i<Names.Count; i++)
{
  res.Add(new string[]{Names[i].ToString(), Phone[i].ToString(), Address[i].ToString()});
}
Sign up to request clarification or add additional context in comments.

3 Comments

you could also do a new object[] or a new class or struct to hold each composite member so you don't have to handle "stringification".
How do i get the informaiton out of the arraylist though?
To get back the values type cast it to string[].
1

Are the contents of these ArrayLists related in some way, such as contacts?

If so, consider creating a class to represent a single contact. This contact class can have Name, PhoneNumber, and Address properties. You can even use a collection for each of these properties in case more than one would apply to a given contact record.

You can store your different contact records into an ArrayList, or if you're using .NET 2+ a strongly typed List.

1 Comment

+1 for mentioning actually creating an object to represent the three fields. C# is an object-oriented language... why not use it as such?
0

If you are operating in .NET 4.0 you should use List<T> though you could still do this with ArrayList too if you like. But you could take advantage of the Tuple to combine your types easily (though I totally agree with the suggestions to create a custom class to hold the "record"):

    List<string> phones = new List<string>();
    List<string> addresses = new List<string>();
    List<string> names = new List<string>();

    // fill with data

    List<Tuple<string,string,string>> results = new List<Tuple<string, string, string>>();

    // aggregate with data - if only two you could also use Zip()
    for (int i = 0; i < names.Count; i++)
    {
        results.Add(Tuple.Create(names[i], addresses[i], phones[i]));
    }

1 Comment

question, how do i get the info out of the Arraylist, since it seems to be in a multi type list?

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.