Is there a way to get an array out of a multidimensional array in C#?
function(int[] me)
{
//Whatever
}
main()
{
int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };
func开发者_JAVA技巧tion(numbers[1]);
}
So here, I'd want to pass int[] {3,4} to the function... but that doesn't work. Is there a way to do that?
I think you want to use a jagged array instead of a 2d array. There is more information here:
http://msdn.microsoft.com/en-us/library/aa288453%28VS.71%29.aspx
an example from that page is:
int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
No you can't do that, but you can convert it by yourself. Something like.
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}
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 (an array of arrays). If you can't change the definition of numbers, you would need to write a function which can
int[] {3,4}
refers to a location, not an array and holds an int. Your function should be
function(int me)
{
//Whatever
}
this is how you will get value and pass to your function
int valueFromLocation = numbers[3,4];
function(valueFromLocation )
{
//Whatever
}
EDIT:
if you need whole array in any case use 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];
now you can pass it the way you want
function(int[] array){}
Perhaps use function(new int[] { 3, 4 });
? It does help if you write an actual question.
精彩评论