如果我们不显式传递数组的大小,数组在哪里初始化?
Where do the Array gets initialized if we do not pass the size of the array explicitly?
我是编程新手。在学习数据结构 Array 时,我开始知道我们必须在创建数组时用大小初始化数组。但我还在我用来学习编码的网站之一中看到了一个代码片段。代码如下
int[] ar = Array.ConvertAll(Console.ReadLine().Split(' '), arTemp => Convert.ToInt32(arTemp))
我猜代码是从控制台读取输入(这是一个字符串)并将其拆分为子字符串,然后转换为整数数组并存储在 'ar'.
我的问题是这一行没有错误。但是在使用它们之前没有提到数组大小。那个怎么样?在这种情况下,该数组的大小在哪里初始化?
Array.ConvertAll()
: Converts an array of one type to an array of another type.
where do the size of this array gets initialized in this case?
在 Array.ConvertAll()
静态函数中,数组大小基于您的输入数组。
在您的例子中,输入数组是 Console.ReadLine().Split(' ')
。 Split(' ')
returns 数组 space 个分隔词。 此数组的大小分配给 Array.ConvertAll()
函数的输出
int[] ar = Array.ConvertAll(Console.ReadLine().Split(' '), arTemp => Convert.ToInt32(arTemp))
//+ +++++++++++++ ++++++++++++++++++++++++ ++++++
//| | | |
//| | | + Integer convertor
//| | |
//| | + Input array with it's size
//| |
//| + Converting input array to array of type int
//|
//+ Output integer array
int ar 是对数组的引用。数组大小在运行时确定。 Array.ConvertAll(...) returns 数组,实际对象。这个新创建的数组链接到 ar 变量。
我是编程新手。在学习数据结构 Array 时,我开始知道我们必须在创建数组时用大小初始化数组。但我还在我用来学习编码的网站之一中看到了一个代码片段。代码如下
int[] ar = Array.ConvertAll(Console.ReadLine().Split(' '), arTemp => Convert.ToInt32(arTemp))
我猜代码是从控制台读取输入(这是一个字符串)并将其拆分为子字符串,然后转换为整数数组并存储在 'ar'.
我的问题是这一行没有错误。但是在使用它们之前没有提到数组大小。那个怎么样?在这种情况下,该数组的大小在哪里初始化?
Array.ConvertAll()
: Converts an array of one type to an array of another type.
where do the size of this array gets initialized in this case?
在 Array.ConvertAll()
静态函数中,数组大小基于您的输入数组。
在您的例子中,输入数组是 Console.ReadLine().Split(' ')
。 Split(' ')
returns 数组 space 个分隔词。 此数组的大小分配给 Array.ConvertAll()
函数的输出
int[] ar = Array.ConvertAll(Console.ReadLine().Split(' '), arTemp => Convert.ToInt32(arTemp))
//+ +++++++++++++ ++++++++++++++++++++++++ ++++++
//| | | |
//| | | + Integer convertor
//| | |
//| | + Input array with it's size
//| |
//| + Converting input array to array of type int
//|
//+ Output integer array
int ar 是对数组的引用。数组大小在运行时确定。 Array.ConvertAll(...) returns 数组,实际对象。这个新创建的数组链接到 ar 变量。