int [,]和int [] []之间的区别是什么? [英] What Is The Difference Between int [,] And int [] [] ?
问题描述
int [,]和int [] []之间的区别是什么?我也想知道为什么它会通过语法提到下面的错误提示
int [,] a = new int [5,2] {{0,0},{1,2},{2,4},{3,6},{4,8}}; //工作正常
int [] [] b = new int [5,2] {{0,0},{1,2},{2, 4},{3,6},{4,8}}; //抛出错误
int [] [] c = {{1,2,3},{11,12,13}}; //抛出错误
What Is The Difference Between int [,] And int [] [] And I Also Want To Know That Why It Throws A Error By Going Through Syntax Mention Below
int[,] a = new int[5, 2] { { 0, 0 }, { 1, 2 }, { 2, 4 }, { 3, 6 }, { 4, 8 } }; // work fine
int[] [] b = new int[5, 2] { { 0, 0 }, { 1, 2 }, { 2, 4 }, { 3, 6 }, { 4, 8 } }; // throws a error
int[][] c = { { 1, 2, 3 }, { 11, 12, 13 } }; // throws error
推荐答案
你弄错了。
You got it wrong.
int[,] a
是一个二维数组
而
is a 2-dimensional array
while
int[][] b
是一个数组数组,即每个元素本身就是一个数组,例如
is an array of arrays, i.e. each element is an array itself, e.g.
int[][] b = new int[5][] { new int[] {1,2}, new int[] {3,4}, new int[] {5,6}, new int[] {7,8}, new int[] {9,0} };
了解更多 https://msdn.microsoft.com/en -us / library / aa288453(v = vs.71).aspx [ ^ ]
以下声明创建一个包含四行的二维数组两列。
The following declaration creates a two-dimensional array of four rows and two columns.
int[,] array = new int[4, 2];
// 2d array
int[,] 2dArray = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
以下是锯齿状数组的声明:单维数组,有三个元素,每个元素其中一个是整数的一维数组:
The following is a declaration of a jagged-array : single-dimensional array that has three elements, each of which is a single-dimensional array of integers:
int[][] jaggedArray = new int[3][];
在使用jaggedArray之前,必须初始化其元素。你可以像这样初始化元素:
Before you can use jaggedArray, its elements must be initialized. You can initialize the elements like this:
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];
每个元素都是一维整数数组。第一个元素是一个5个整数的数组,第二个是4个整数的数组,第三个是2个整数的数组。
从这里:
多维数组 [ ^ ]
锯齿状阵列 [ ^ ]
-KR
Each of the elements is a single-dimensional array of integers. The first element is an array of 5 integers, the second is an array of 4 integers, and the third is an array of 2 integers.
From here:
Multidimensional Arrays[^]
Jagged Arrays[^]
-KR
区别在于[ ,]是一个2D固定大小的数组,包含正好x * y个元素,当你在new
赋值中分配空格时给出x和y:
The difference is that [,] is a 2D fixed size array containing exactly x * y elements, where x and y are given when you allocate the space in thenew
assignment:
int[,] arr = new int[x,y];
[] []创建一个锯齿状的arra y,你一次只分配一个维度:
[][] creates a jagged array, where you only assign one dimension at a time:
int[][] arr = new int[x][];
for(int i = 0; i < x; i++)
{
arr[i] = new int[(i + 1) * 2];
}
锯齿状数组的每个元素可以是不同的大小,需要使用单独的 new
语句进行分配。 />
虽然您可以使用以下语法初始化固定大小的数组:
Each element of the jagged array can be a different size, and needs to be allocated with a separate new
statement.
While you can initialize fixed size arrays with this syntax:
int[,] arr = new int[5, 2] { { 0, 0 }, { 1, 2 }, { 2, 4 }, { 3, 6 }, { 4, 8 } };
你需要这个语法来做一个锯齿状数组:
You need this syntax to do the same with a jagged array:
int[][] arr = new int[5][] { new int[] { 0, 0 },
new int[] { 1, 2 },
new int[] { 2, 4 },
new int[] { 3, 6 },
new int[] { 4, 8 } };
这篇关于int [,]和int [] []之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!