[英]Add an item to an already existing 2D integer array in a function in windows form C#
我有一個二維整數數組,我想根據我在方法中得到的值在其中添加一個項目。
private int[,] indexes = new int[100,2];
這是聲明的數組,下面是我根據索引在數組中添加項目的方式,但是在我的方法中,我不知道確切的索引。 有沒有一種方法可以獲取數組中最后一個元素的索引,然后向其添加元素,或者可以直接在現有數組的末尾添加一個方法?
indexes[0,0]= currRowIndex;
indexes[0,1] = 0;
在這里,我在索引0處添加了。類似地,我應該能夠在數組元素結束處添加到最后一個索引。
考慮使用嵌套名單- List<List<int>>
從MSDN 名單
然后,新值將始終添加到集合的末尾
List<List<int>> indexes = new List<List<int>>();
indexes.Add(new List<int> { 1, 2 });
並按索引檢索值
int firstValue = indexes[0][0];
int secondValue = indexes[0][1];
indexes .GetLength(0) -> Gets first dimension size
indexes .GetLength(1) -> Gets second dimension size
因此您可以將項目添加為
indexes[indexes .GetLength(0),
indexes .GetLength(1)] = value;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.