简体   繁体   中英

Add an item to an already existing 2D integer array in a function in windows form C#

I have a 2D integer array and i want to add an item in it as per the values i get in the method.

  private  int[,]  indexes = new int[100,2];

This is the array declared and below is how i add items in the array as per the indexes but in my method i would not know the exact indexes. Is there a way where in i can get the indexes of the last element in the array and than add an element to it or a way where i can add directly at the end of the existing array

 indexes[0,0]= currRowIndex;
 indexes[0,1] = 0;

Here i have added at the index 0. Similarly i should be able to add to the last index where the elements in an array ends.

Consider of using nested lists - List<List<int>> From MSDN List
Then new values will be added always to the end of collection

List<List<int>> indexes = new List<List<int>>();

indexes.Add(new List<int> { 1, 2 });

And retrieve value by index

int firstValue = indexes[0][0];
int secondValue = indexes[0][1];
indexes .GetLength(0)  -> Gets first dimension size

indexes .GetLength(1)  -> Gets second dimension size

so you can add items to the list as

indexes[indexes .GetLength(0), 
indexes .GetLength(1)] = value; 

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