Reputation: 441
I thought this was straight forward, but given a multi-dimensional array
string[,] table = new string[,]
{
{"Apple", "Banana", "Clementine", "Damson"},
{"Elderberry", "Fig", "Grape", "Huckleberry"},
{"Indian Prune", "Jujube", "Kiwi", "Lime"}
};
How can I return a specific array using an index?
I tried the following but it does not work.
string[] firstArray = table[0];
Thank you.
Upvotes: 1
Views: 808
Reputation: 720
You can use jagged array like this:
string[][] table = new string[3][]
{
new string[] {"Apple", "Banana", "Clementine", "Damson"},
new string[] {"Elderberry", "Fig", "Grape", "Huckleberry"},
new string[] {"Indian Prune", "Jujube", "Kiwi", "Lime"}
};
string[] array = table[0];
OR
if you are not interested in using jagged array, you can use extension method like:
public static class MyExtensions
{
public static string[] GetArray(this string[,] table, int dimension )
{
string[] array = new string[table.GetLength(dimension)];
for (int i = 0; i < array.Length; i++)
{
array[i] = table[dimension, i];
}
return array;
}
}
then you get any dimension by passing your desired dimension:
string[] firstArray = table.GetArray(0);
hope this helps
Upvotes: 1
Reputation: 9709
Forr you the best approach is an extension method on array.Then you can invoke it in everywhere. see below.Since you may have many such arrays in your project once you implement an extension method you can apply to any array as table.method(row)
public static class ext{
public static string[] twodim(this string[,] inarr, int row) {
string[] ret = new string[inarr.GetLength(1)];
for (int i = 0; i < inarr.GetLength(1); i++)
ret[i] = inarr[row, i];
return ret;
}
}
public class Program{
static void dump(string name, string[] arr){
Console.WriteLine(name);
for (int i = 0; i < arr.Length; i++)
Console.WriteLine(" {0} ", arr[i]);
}
static void Main(string[] args){
string[,] table = new string[,] {
{"Apple", "Banana", "Clementine", "Damson"},
{"Elderberry", "Fig", "Grape", "Huckleberry"},
{"Indian Prune", "Jujube", "Kiwi", "Lime"}
};
dump("Row 0", table.twodim(0));
dump("Row 0", table.twodim(1));
dump("Row 0", table.twodim(2));
}
Upvotes: 1
Reputation: 16597
The simple way,
string[,] table = new string[,]
{
{"Apple", "Banana", "Clementine", "Damson"},
{"Elderberry", "Fig", "Grape", "Huckleberry"},
{"Indian Prune", "Jujube", "Kiwi", "Lime"}
};
string[] arr = new string[table.GetLength(1)];
for (int index = 0; index < table.Length; index++)
{
arr[index] = table[0,index];
}
Jagged arrays are also an option.
Upvotes: 0
Reputation: 6797
If you want that particular syntax, you need to use jagged arrays AFAIK.
string[][] table = new string[][]
{
new string[]{"Apple", "Banana", "Clementine", "Damson"},
new string[]{"Elderberry", "Fig", "Grape", "Huckleberry"},
new string[]{"Indian Prune", "Jujube", "Kiwi", "Lime"}
};
string[] firstArray = table[0];
The two-dimensional array you've specified requires table[0, N] to retrieve a single element from the table.
Upvotes: 0
Reputation: 1233
There is a difference between multi-dimensional arrays and jagged arrays in C#. Had your array been defined like this:
string[][] table = new string[][]
{
new string[] {"Apple", "Banana", "Clementine", "Damson"},
new string[] {"Elderberry", "Fig", "Grape", "Huckleberry"},
new string[] {"Indian Prune", "Jujube", "Kiwi", "Lime"}
};
then your line would have worked. To get a row out of a multi-dimensional array, you can do something like this:
string[] firstRow = new string[table.GetLength(1)];
for (int i = 0; i < firstRow.Length; i++)
{
firstRow[i] = table[0, i];
}
Upvotes: 1