Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]["lastname"]);
console.writelen(array[x]["phone"]);
like image 743
Data-Base Avatar asked May 04 '10 08:05

Data-Base


2 Answers

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 };
like image 110
Will Vousden Avatar answered Oct 05 '22 11:10

Will Vousden


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.

like image 40
mafu Avatar answered Oct 05 '22 10:10

mafu