user3300727
user3300727

Reputation: 101

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.

Upvotes: 1

Views: 39

Answers (2)

Fabio
Fabio

Reputation: 32445

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];

Upvotes: 1

Brijesh Bhakta
Brijesh Bhakta

Reputation: 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; 

Upvotes: 0

Related Questions