简体   繁体   中英

Declaring a 2D array without knowing its size in C#

I would like to use a 2-D array but I can't know its size in advance. So my question is: how to declare it? And then how to add values into it?

String[,] tabConfig = new String[?, 4];
foreach(blabla with i)
{
    tabConfig[i, 0] = a;
    tabConfig[i, 1] = b;
    tabConfig[i, 2] = c;
    tabConfig[i, 3] = d;
}

I know I can also use a list but I am not very familiar with it.

Thank you!

EDIT: Brace yourselves! Here come my true code with Jon Skeet's help!

List<string[]> tabConfig = new List<string[]>();
String[] temp = new String[4];//The array that will be inside the List
int line = 0, column = 0;

foreach (XmlNode e in doc.DocumentElement.ChildNodes)
{
    if (e.Attributes["Server"].Value == choice)
    {
        temp[0] = e.Attributes["Serveur"].Value;//Here is value 'a'

        column = 1;
        foreach (XmlNode i in e.ChildNodes)
        {
            temp[colonne] = i.InnerText;//Here are values 'b', 'c' and 'd'
            column++;
        }
        tabConfig.Add(temp);//Put a new line into the List
        line++;
    }
}

And to call it:

foreach(string[] array in tabConfig)
    foreach(String txt in array)
        Console.WriteLine(txt);

So my question is: how to declare it?

You can't. All arrays in .NET are fixed size: you can't create an array instance without knowing the size.

It looks to me like you should have a List<T> - and I'd actually create a class to hold the four properties, rather than just using four elements of an array, probably. You could use a list of arrays:

List<string[]> tabConfig = new List<string[]>();
foreach (...)
{
    tabConfig.Add(new string[] { a, b, c, d });
}

... but then you need to know what those four elements mean in terms of ordering etc, which is likely to make the rest of your code harder to understand.

You can use a

List<List<string>> 

for this, dont' you?

You are probably better of using a dictionary something like

IDictionary<string, IList<TYPE>> myContainer = new Dictionary<string, List<TYPE>>();
myContainer.Add("key1", new List<TYPE>());
myContainer["key1"].Add("SomeTYPE-1");
myContainer["key1"].Add("SomeTYPE-2");
myContainer["key1"].Add("SomeTYPE-3");

myContainer.Add("key2", new List<TYPE>());
myContainer["key2"].Add("A");
myContainer["key2"].Add("B");
myContainer["key2"].Add("C");

Update As commented by @jon you use a dictionary if you are wanting to access elements of your 2d array by some form of key, which may / may not be useful in your situation. If you don't want a key based dictionary then stick with what @jon posted.

If you can do a foreach, you can know the size of it. If you know the size, you can create the aray with the necessary size.

But I would use a List for it. It's friendlier for that type of situation.

http://www.functionx.com/csharp/builtinclasses/list.htm

Workaround:

private struct MyStruct
{
    int x;
    int y;
}

List<MyStruct> myList = new List<MyStruct>(); 

Make a class like this

public class MyClass
{
public string one{get;set;}
public string two{get;set;}
}

Now you can make a list of this object like

List<MyClass> list1=new List<MyClass>();

如果我是你,我会把你正在迭代的列表的长度作为数组大小,因为数组必须以固定的大小声明

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