开发者

C# multi-dimensional array, ArrayList, or hash table?

I'm trying to figure out how to build a multi-dimensional "array" that is:

  • flexible size
  • use 2 keys
  • 1st key is int (flexible)
  • 2nd key is string (kind of limited)

The use will be like:

console.writelen(array[0]["firstname"]);
console.writelen(array[0]["lastname"]);
console.writelen(array[0]["phone"]);

console.writelen(array[1]["firstname"]);
console.writelen(array[1]["lastname"]);
console.writelen(array[1]["phone"]);

.....
.....

console.writelen(array[x]["firstname"]);
console.writelen(array[x]["las开发者_如何转开发tname"]);
console.writelen(array[x]["phone"]);


Are you sure it wouldn't be more appropriate to create a class/struct to contain the data? For example:

class Person
{
    public string FirstName
    {
        get;
        set;
    }

    public string LastName
    {
        get;
        set;
    }

    public string Phone
    {
        get;
        set;
    }
}

Then you'd just create an array of Person:

var array = new Person[1];
array[0] = new Person() { FirstName = "Joe", LastName = "Smith", Phone = "foo" };

Or, since you say "flexible size", maybe you'd want a list instead:

var list = new List<Person>();
list.Add(new Person());

Update: The syntax used to set array[0] in the first example is an object initializer; the following two snippets are roughly equivalent:

var foo = new Person();
foo.FirstName = "John";

var bar = new Person() { FirstName = "John" };

So yes, you could just call list.Add(new Person() { ... }) if you wanted to. You could also make use of collection initializers in this case:

var john = new Person() { FirstName = "John" };
var joe = new Person() { FirstName = "Joe" };
var list = new List<Person>() { john, joe };


You can simply use this:

Dictionary<int, Dictionary<string, string>>

Use it like this:

var dd = new Dictionary<int, Dictionary<string, string>>();
dd[5] = new Dictionary<string, string>();
dd[5]["a"] = "foo";

You can also create a new class to simplify the creation of the inner dictionary:

class DDict { // optional: generic
    private readonly Dictionary<int, Dictionary<string, string>> _Inner = new ...;

    public Dictionary<string, string> this (int index) {
        Dictionary<string, string> d;
        if (!_Inner.TryGetValue(index, out d)) {
             d = new Dictionary<string, string>();
             _Inner.Add(index, d);
        }
        return d;
    }
}

var dd = new DDict();
dd[5]["a"] = "hi";

If the first index is sequential, you can of course also just use an array of dictionaries:

var dd = new Dictionary<string, string>[128];

Also, if the inner members are always the same, I suggest to create a new class and access it in an array:

class Dat {
    string name;
    string phone;
}
var list = new Dat[128]

// access:
list[5].name = "matt";

Instead of an array, you could also use a List or a Dictionary<int, Dat> in that case.


I don't believe you can do this with an Array unless you have a single Array of KeyValuePair<int,string>, but I think you really want a Dictionary<int,string>.

var dic = new Dictionary<int,string>();
dic[0] = "zero";
dic[1] = "one";
dic[2] = "two";

foreach(KeyValuePair<int,string> kvp in dic)
{
   Console.WriteLine(String.Format("Key: {0}, Value: {1}",kvp.Key,kvp.Value);

}


Actually i just see two dimensions. The first one is a row index, the second is a column index. And this sounds like a DataTable to me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜