ruby中的多维数组问题

时间:2022-04-30 21:34:03

ruby 1.8.7 (2008-08-11 patchlevel 72) [i586-linux]

ruby 1.8.7(2008-08-11 patchlevel 72)[i586-linux]

From script/console:

>> pairs = Array.new(2).map!{Array.new(2).map!{Array.new(2, Array.new)}}
=> [[[[], []], [[], []]], [[[], []], [[], []]]]
>> pair = Pair.first
=> #<Pair id: 39, charge_card_id: 1, classroom_id: 1, timeslot_id: 1, created_at: "2010-04-01 00:45:37", updated_at: "2010-04-01 00:45:47">
>> pairs[0][0][0] << pair
=> [#<Pair id: 39, charge_card_id: 1, classroom_id: 1, timeslot_id: 1, created_at: "2010-04-01 00:45:37", updated_at: "2010-04-01 00:45:47">]
>> pairs[0][0]
=> [[#<Pair id: 39, charge_card_id: 1, classroom_id: 1, timeslot_id: 1, created_at: "2010-04-01 00:45:37", updated_at: "2010-04-01 00:45:47">], [#<Pair id: 39, charge_card_id: 1, classroom_id: 1, timeslot_id: 1, created_at: "2010-04-01 00:45:37", updated_at: "2010-04-01 00:45:47">]]
>>

So the question is why the pair object appears in pairs[0][0][0] AND in pairs[0][0][1] inspite of I did'n ask it to appear there. Notice I don't nedd to pairs[0][0][0] = pair - I want it as a first array member, so I need to use << or .push.

所以问题是为什么成对对象出现在成对[0] [0] [0]和成对[0] [0] [1]中,尽管我没有要求它出现在那里。请注意,我不需要对[0] [0] [0] =对 - 我希望它作为第一个数组成员,所以我需要使用< <或.push。< p>

3 个解决方案

#1


3  

First of all, you want

首先,你想要的

pairs = Array.new(2) { Array.new(2) { Array.new(2) { [] }}}

instead of what you got. Two major differences:

而不是你得到的。两个主要差异:

  • you save yourself the #map! calls
  • 你自己保存了#map!电话

  • in your example, "Array.new(2, Array.new)" is creating one Array which is used for both indices, so you are refering to the same array twice. By using the block syntax, you are ensuring that for every index you are having one separate instance of Array
  • 在您的示例中,“Array.new(2,Array.new)”正在创建一个用于两个索引的数组,因此您将两次引用相同的数组。通过使用块语法,您可以确保每个索引都有一个单独的Array实例

#2


0  

Now works with:

现在适用于:

pairs = Array.new(2).map!{Array.new(2).map!{Array.new(2).map!{Array.new}}}

I think it's because of the deepest arrays was just links to memory pointer.

我认为这是因为最深的数组只是指向内存指针的链接。

#3


0  

Array.new(2, Array.new) gives you an array of size 2, with a copy of the same empty array object at both index 0 and index 1. If you want them to be different objects, use map! like you've done at the higher levels.

Array.new(2,Array.new)为您提供了一个大小为2的数组,在索引0和索引1处都有相同的空数组对象的副本。如果您希望它们是不同的对象,请使用map!就像你在更高层次做的那样。

#1


3  

First of all, you want

首先,你想要的

pairs = Array.new(2) { Array.new(2) { Array.new(2) { [] }}}

instead of what you got. Two major differences:

而不是你得到的。两个主要差异:

  • you save yourself the #map! calls
  • 你自己保存了#map!电话

  • in your example, "Array.new(2, Array.new)" is creating one Array which is used for both indices, so you are refering to the same array twice. By using the block syntax, you are ensuring that for every index you are having one separate instance of Array
  • 在您的示例中,“Array.new(2,Array.new)”正在创建一个用于两个索引的数组,因此您将两次引用相同的数组。通过使用块语法,您可以确保每个索引都有一个单独的Array实例

#2


0  

Now works with:

现在适用于:

pairs = Array.new(2).map!{Array.new(2).map!{Array.new(2).map!{Array.new}}}

I think it's because of the deepest arrays was just links to memory pointer.

我认为这是因为最深的数组只是指向内存指针的链接。

#3


0  

Array.new(2, Array.new) gives you an array of size 2, with a copy of the same empty array object at both index 0 and index 1. If you want them to be different objects, use map! like you've done at the higher levels.

Array.new(2,Array.new)为您提供了一个大小为2的数组,在索引0和索引1处都有相同的空数组对象的副本。如果您希望它们是不同的对象,请使用map!就像你在更高层次做的那样。