简体   繁体   English

如何在2D数组中动态添加值?

[英]How to dynamically add values in a 2D array?

I am working with 2D arrays. 我正在使用2D数组。 what I want, is to dynamically add elements in specific columns of my 2D array named symboltable2 . 我想要的是在名为symboltable2 2D数组的特定列中动态添加元素。 I have been doing it as; 我一直这样做;

result is another 1D array in which there are certain words: 结果是另一个1D数组,其中有一些单词:

string[,] symboltable2 = new string[,];

if (result.Contains("int")) {
    for (int todynamic = 0; todynamic < result.GetLength(0); todynamic++) {
        symboltable2[todynamic, 6] = "int";
    }
    for (int sym2 = 0; i < symboltable1.GetLength(0); sym2++) {
        f4.listBox6.Items.Add(symboltable1[sym2, 5]); // To show if the values are added or not
    }
} 

but the code above isn't giving me any results... kindly help :( 但上面的代码没有给我任何结果...请帮助:(

You need to set the size of the array. 您需要设置数组的大小。 And to have it public I would use a property and initialize your array in the class constructor like this: 为了公开我会使用属性并在类构造函数中初始化您的数组,如下所示:

public class MyClass
{
    public string[,] symboltable2 { get; set; } 

    public MyClass()
    {
        symboltable2 = new string[10,10];
    }

            // ...

while implementing arrays you need to give the array's dimension ie 在实现数组时,您需要给出数组的维度,即

string[,] sa = new string[5,15];

or 要么

string[,] sa = new string[listString1.Count, listString2.Count] 

about adding / changing elements to 2D array.. as a simple string array example : 关于添加/更改元素到2D数组...作为一个简单的字符串数组示例:

sa[0, 1] = "a";
sa[0, 2] = "b";
sa[1, 0] = "Istanbul / Turkey";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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