简体   繁体   中英

how to display 2D array in C# windows form?

I don't know how to display it on a listboxt when I try to put code to display it error always occurs

int arrayRows = 5;
int arrayCols = 3;

int[,] arrayTimes;
arrayTimes = new int[arrayRows , arrayCols];
int mult = 0;

for (int i = 0; i != arrayRows; i++)
{
    mult = mult + 10;
    for (int j = 0; j != arrayCols; j++)
    {
        arrayTimes[i, j] = mult;
        mult = mult * 10;
    }

    mult = mult / 1000;
    listBox1.Items.Add("arrayPos= " + i + "values =" + arrayTimes[i,j]);            
}

Try doing it like this :

int arrayRows = 5;
int arrayCols = 3;

int[,] arrayTimes;
arrayTimes = new int[arrayRows , arrayCols];
int mult = 0;

for (int i = 0; i != arrayRows; i++)
{
    mult = mult + 10;
    for (int j = 0; j != arrayCols; j++)
    {
        arrayTimes[i, j] = mult;

        listBox1.Items.Add("arrayPos= " + i + "values =" + arrayTimes [i,j]);

        mult = mult * 10;
    }
    mult = mult / 1000;
}

You are using j after you get out of the for (int j = 0; j != arrayCols; j++){ ... } structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM