0

I will add the values in the String[] in to the Arraylist. But, I want to access those string values from the ArrayList.

I tried this way.

private void Form1_Load()
{
    fr = new string[5] { "1", "2", "3", "4", "5" };
    bd = new string[5] {"a", "b","c", "d", "e"};
    m = new ArrayList();
    dosomething();
}

private void dosomething()
{
    string[] record = new string[3];
    for (int i = 0; i < 5; i++)
    {
        record[0] = "1";
        record[1] = fr[i];
        record[2] = bd[i];
        m.Add(record);
    }
}

I don't want to use the for loop is that any other way to do this???

7
  • Is your used programming language Java? Commented May 23, 2013 at 13:17
  • @reporter : private void Form1_Load(object sender, EventArgs e) -> no it looks like .Net Commented May 23, 2013 at 13:20
  • why did this get the android tag? Commented May 23, 2013 at 13:20
  • Not in Java. I need C# Commented May 23, 2013 at 13:20
  • updated mine to be C# Commented May 23, 2013 at 13:21

3 Answers 3

0

I recommend you to use dictionaries. It is in my opinion the quickest way to store / access data. Also, with arraylists, at runtime, it performs a dynamic transtyping, which makes you loose so much time.

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

Comments

0

You maybe want to use :

        fr = new string[5] { "1", "2", "3", "4", "5" };
        bd = new string[5] { "a", "b", "c", "d", "e" };
        m = new ArrayList();
        fr.ToList().ForEach(_item => m.Add(new String[]{ "1", _item,bd[fr.ToList().IndexOf(_item)]}));

But I would prefere a solution like Fares already recommented...Use A Dictionary

Dictionary - MSDN

Comments

0

Not sure why you need an ArrayList. A generic list might be more suitable for you.

var fr = new string[5] { "1", "2", "3", "4", "5" };
var bd = new string[5] {"a", "b","c", "d", "e"};

int i = 1;
var results = fr.Zip<string, string, string[]>(bd, (a,b) => {
    var v = new string [3] { i.ToString(), a,b }; 
    i++;
    return v;
}).ToList();

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.