Vinícius Albino
Vinícius Albino

Reputation: 547

Multidimensional List C#

I need to create a multidimensional guard List three values​​, X, Y and Z, and I need a List that is because once the value is queried, the array must be removed.

The query would look something like this: List [0] [0] = X, List [0] [a] = Y and List [0] [2] = X, so that I can remove only the index 0 and he already remove all the other three.

Upvotes: 4

Views: 9498

Answers (4)

psubsee2003
psubsee2003

Reputation: 8751

I am not sure I am following your logic as you are using both strings and integers as your second indexer, and referencing X twice, but not Z. Assuming these are typos, I am going to take a guess at what you want.

Have you considered a custom type with X, Y, AND Z properties, and an indexer to give you the behavior you described:

You also don't mention what types your values are, so I am using object, but feel free to substitute your own type (or a generic type)

public class MyType
{
    private object[] backingArray = new object[3];
    public object this[int index]
    {
        get { return backingArray[index]; }
        set { backingArray[index] = value; }
    }

    public object X 
    {
        get { return backingArray[0]; }
        set { backingArray[0] = value; }
    }
    public object Y
    {
        get { return backingArray[1]; }
        set { backingArray[1] = value; }
    }
    public object Z 
    {
        get { return backingArray[2]; }
        set { backingArray[2] = value; }
    }
}

You could then use it like this:

List<MyType> list = new List<MyType>();
list = PopulateList();  // fill list with values


var x = list[0][0];
var y = list[0][1];
var z = list[0][2];

Of course, this implementation depends on your 2nd dimension always consisting of 3 elements. If it will not be consistent, then one of the other answers abound for your needs.

Upvotes: 0

Dan Rigby
Dan Rigby

Reputation: 17893

If you need to create a multidimensional list, you can always create a list of lists like so:

var multiDimensionalList = new List<List<string>>{
    new List<string>{"A","B","C"},
    new List<string>{"D","E","F"},
    new List<string>{"G","H","I"},
};
Console.WriteLine(multiDimensionalList[2][1]); // Prints H

multiDimensionalList[2].RemoveAt(1);
Console.WriteLine(multiDimensionalList[2][1]); // Prints I

multiDimensionalList[2][1] = "Q";
Console.WriteLine(multiDimensionalList[2][1]); // Prints Q

Be aware though that attempting to replace a value that doesn't exist by way of assignment will throw an exception:

multiDimensionalList[2][5] = "R"; // Throws an ArgumentOutOfRangeException

Upvotes: 4

Robert Snyder
Robert Snyder

Reputation: 2409

I'm sorry but your question is a little bit hard to understand, but I will take a stab at it. Please don't interchange the words Array and List as they are different yet related ideas in C#. I believe that you mean Array with your use of [] brackets. Although you might want to consider using lists as they have a nice way to remove certain elements from a list by using the element. the MSDN has some good information as to how you might proceed. List(T).Remove method

the list will restructure it self to remove or add elements as desired.

Upvotes: 0

Giscard Biamby
Giscard Biamby

Reputation: 4619

Your question is very hard to understand, but perhaps what you are looking for can be accomplished with an array of arrays? This is how multidimensional arrays are implemented in some languages anyways.

In your case you might be using a List of List's: List>. And this would satisfy your requirement to remove "all the other three" by removing the first element in the outer List<> object.

Upvotes: 0

Related Questions