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!

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);
            }
        }

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

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);
            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.