I can't find anything about this so I'm not sure if it is possible but I have a tuple that contains the coordinates of an element in a two-dimensional array. I want to be able to find the distance between elements in a two-dimensional array and to do this I want the position of the element in one dimensional array form (I'm not sure of a better way to do this). So is it possible to turn a Tuple into an array?
This is the array:
string[,] keypad = new string[4, 3]
{
{"1", "2", "3"},
{"4", "5", "6"},
{"7", "8", "9"},
{".", "0", " "}
};
This is the method I used to get the coordinates of an element in a multidimensional array:
public static Tuple<int, int> CoordinatesOf<T>(this T[,] matrix, T value)
{
int w = matrix.GetLength(0); // width
int h = matrix.GetLength(1); // height
for (int x = 0; x < w; ++x)
{
for (int y = 0; y < h; ++y)
{
if (matrix[x, y].Equals(value))
return Tuple.Create(x, y);
}
}
return Tuple.Create(-1, -1);
}
If i understand you well, you want to convert Tuple<int, int>
into an array...
As i mentioned in the comment to the question, MSDN documentation explains exactly what Tuple<T1, T2>
is. A 2-tuple is a pair or KeyValuePair<TKey, TValue>
structure...
//create a 2-tuple
Tuple<int, int> t = Tuple.Create(5,11);
//pass Item1 and Item2 to create an array
int[] arr = new int[]{t.Item1, t.Item2};
For further details, please see:
Introduction to Tuples in .NET Framework 4.0
Overview: Working with Immutable Data