0

Hello,

I have a problem to add string arrays into the ArrayList.
My data is recorded as: {"1","0","a"}, {"1","1","b"}, ....

I'm going through the "for" loop and trying to store each of these records into the ArrayList, but, after it stores all records, it appears that all elements in the ArrayList are the same!

Here's an example of code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Generic;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        string[] fr;
        string[] bd;

        ArrayList m;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            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);

            }
            
        }
    }
}

All records in m are equal to: {"1","5","e"}. WHY?
Thanks!

Edited by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by micka10
1

Hey Micka10

Its becuase your arraylist contains a reference to string[] record, which you have only created once (in memory). You are simply adding a reference to that array multiple times and at the same time updating it.

To solve it simple create a new array inside the loop.

i.e

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

Edited by Tower-Rounder: n/a

0

Because each element of "m" will reference the same "record" object, which will contain whatever the last items added to the record are. You need to move the creation of the "record" into the loop so that each "record" of "m" is a different object:

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

EDIT: Tower-Rounder already replied before i got to it...

Edited by DdoubleD: n/a

0

Wonderful!
Thank you Tower-Rounder!!!

Hey Micka10

Its becuase your arraylist contains a reference to string[] record, which you have only created once (in memory). You are simply adding a reference to that array multiple times and at the same time updating it.

To solve it simple create a new array inside the loop.

i.e

private void dosomething()
        {
            for (int i = 0; i < 5; i++)
            {
                string[] record = new string[3];
                record[0] = "1";
                record[1] = fr[i];
                record[2] = bd[i];
                m.Add(record);
            }
        }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.