二维数组初始化 [英] Two dimensional array initialization
问题描述
我可以做一个简单的初始化工作:
Object [,] ONE_ROW = {{" Vodafone" ;,5550160100197016}};
但是,现在我想创建另一个包含ONE_ROW的多个副本的数组(不会编译):
Object [,] TWO_ROWS = {ONE_ROW,ONE_ROW}; //在Java中工作;-)
这会产生结构不正确的数组初始值设定项!!
但是,这是正确的使用硬编码数据的实现:
Object [,] TWO_ROWS = {{" Vodafone"," 5550160100197016"},{" Vodafone"," 5550160100197016"}} ;
有什么想法吗?
问候,
Dadi。
Dadi< da ** @ hugur.is>写道:我可以做一个简单的初始化工作:
对象[,] ONE_ROW = {{" Vodafone"," 5550160100197016"}};
听起来你想要锯齿状阵列,而不是矩形
阵列。如果你这样做:
Object [] [,] TWO_ROWS = {ONE_ROW,ONE_ROW};我相信它会工作的。
(虽然哈文没试过......)
-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件
< blockquote>其实Jon我认为他想要
Object [] [] TWO_ROWS =
a jagged ONE_ROW数组的数组。你的代码将是一个参差不齐的2D
矩形阵列,我认为这不是意图,不是吗?
我'我之前对阵列的错误也许是错的。
Jon Skeet [C#MVP]" < SK *** @ pobox.com>在消息中写道
新闻:MP ************************ @ msnews.microsoft.c om ...Dadi< da ** @ hugur.is>写道:我可以做一个简单的初始化工作:
对象[,] ONE_ROW = {{" Vodafone"," 5550160100197016"}};
这听起来像你想要锯齿状阵列,而不是矩形阵列。如果你这样做:
对象[] [,] TWO_ROWS = {ONE_ROW,ONE_ROW};我相信它会起作用。
(虽然没有尝试过......)
-
Jon Skeet - < sk ** *@pobox.com>
http://www.pobox.com/~双向飞碟
如果回复小组,请不要给我发邮件
Daniel Billingsley< db ***** *****@NO.durcon.SPAAMM.com>写道:其实Jon我认为他想要
对象[] [] TWO_ROWS =
一个ONE_ROW数组的锯齿状阵列。你的代码是一个参差不齐的2D矩形阵列,我不认为这是意图,不是吗?
我不喜欢不知道 - 意图不是很清楚!但是,我的代码将会给出一个2D矩形阵列数组。
-
Jon Skeet - < sk ***@pobox.com>
http://www.pobox.com /〜双向飞碟
如果回复小组,请不要给我发邮件
Hi,
I can make a simple initialization work like this:
Object[,] ONE_ROW = {{"Vodafone", "5550160100197016"}};
But, now I want to create another array that consists of multiple copies of ONE_ROW like this (will not compile):
Object[,] TWO_ROWS = {ONE_ROW, ONE_ROW}; // Works in Java ;-)
This yields "Incorrectly structured array initializer"!!
This, however, is a correct implementation using hard-coded data:
Object[,] TWO_ROWS = {{"Vodafone", "5550160100197016"}, {"Vodafone", "5550160100197016"}};
Any ideas?
Regards,
Dadi.解决方案Dadi <da**@hugur.is> wrote:I can make a simple initialization work like this:
Object[,] ONE_ROW = {{"Vodafone", "5550160100197016"}};
But, now I want to create another array that consists of multiple copies
of ONE_ROW like this (will not compile):
It sounds like you want jagged arrays then, rather than rectangular
arrays. If you do:
Object[][,] TWO_ROWS = {ONE_ROW, ONE_ROW}; I believe it''ll work.
(Haven''t tried it though...)
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Actually Jon I think he wants
Object[][] TWO_ROWS =
a jagged array of ONE_ROW arrays. Your code would be a jagged array of 2D
rectangular arrays which I don''t think was the intent, wouldn''t it?
I''ve been wrong about arrays before so maybe I am again.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...Dadi <da**@hugur.is> wrote:I can make a simple initialization work like this:
Object[,] ONE_ROW = {{"Vodafone", "5550160100197016"}};
But, now I want to create another array that consists of multiple copies
of ONE_ROW like this (will not compile):
It sounds like you want jagged arrays then, rather than rectangular
arrays. If you do:
Object[][,] TWO_ROWS = {ONE_ROW, ONE_ROW}; I believe it''ll work.
(Haven''t tried it though...)
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Daniel Billingsley <db**********@NO.durcon.SPAAMM.com> wrote:Actually Jon I think he wants
Object[][] TWO_ROWS =
a jagged array of ONE_ROW arrays. Your code would be a jagged array of 2D
rectangular arrays which I don''t think was the intent, wouldn''t it?
I don''t know - the intent wasn''t very clear! However, my code would
give a single array of 2D rectangular arrays.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
这篇关于二维数组初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!