Is there a way to get an array from a multidimensional array in C #?

function(int[] me)
{
    //Whatever
}

main()
{
    int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };    
    function(numbers[1]);
}

So, here I want to pass the int [] {3,4} function to the function ... but this does not work. Is there any way to do this?

+3
source share
5 answers

I think you want to use a jagged array instead of a 2d array. There is additional information here:

http://msdn.microsoft.com/en-us/library/aa288453%28VS.71%29.aspx

example from this page:

int [] [] numbers = new int [2] [] {new int [] {2,3,4}, new int [] {5,6,7,8,9}};

+2
source

int[] {3,4}refers to a location , not an array, and contains an int. Your function should be

function(int me)
{
    //Whatever
}

this is how you get the value and go to your function

    int valueFromLocation = numbers[3,4];

    function(valueFromLocation )
    {
        //Whatever
    }

EDIT:

, Jagged Arrays

int[][] jaggedArray =
     new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };

int[] array1 =  jaggedArray[1];
int[] array1 =  jaggedArray[2];

, .

function(int[] array){}
0

, , . - .

 int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {8, 6} };

           List<int[]> arrays = new List<int[]>();
           for (int i = 0; i < 3; i++)
           {
               int[] arr = new int[2];
               for (int k = 0; k < 2; k++)
               {
                   arr[k] = numbers[i, k];
               }

               arrays.Add(arr);
           }

           int[] newArray = arrays[1]; //will give int[] { 3, 4}
0
function(int[] me) 
{ 
    //Whatever 
} 

main() 
{ 
    int[][] numbers = new int[3][] { new int[2] {1, 2}, new int[2]{3, 4}, new int[2] {5, 6} };     
    function(numbers[1]); 
} 

This is called a jagged array (array of arrays). If you cannot change the definition of numbers, you will need to write a function that can

0
source

Perhaps use function(new int[] { 3, 4 });? This will help if you write a real question.

0
source

Source: https://habr.com/ru/post/1733471/


All Articles