简体   繁体   中英

How can I return a specific array using an index in a multi-dimensional array in .NET?

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.

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

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];
  }

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

  }

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM