views:

45

answers:

5

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.

+1  A: 

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

Matin Habibi
I am actually converting a large Java project and don't want to rewrite all array declerations so is there a way to use my previous array decleration? Thanks.
bahith
@bahith: Java doesn't even have multidimensional arrays, so a jagged array would be the most straightforward conversion from Java code, actually.
Joey
@Johannes, thank you very much for this clarification.
bahith
+1  A: 

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];
  }
Tor Livar
Thnks. But please see my reply to Matin.
bahith
A: 

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.

A: 

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.

Incognito
A: 

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));

  }
josephj1989