我有一个错误。本质上,我正在将2d数组复制到另一个数组。 (因此arrayCopy = $ array)
并且我正在尝试将数组旋转90度,如果这有意义吗?这是一个有助于说明的图表。 https://gyazo.com/2183552cfe539406b3197e6329f9ba28。
我正在尝试的代码是此https://gyazo.com/61669116735ca96ce52115442a44e36c。
本质上,我正在遍历数组的大小并“旋转”数组。
但是我在https://gyazo.com/d44d558987f7b7ae0b06678d76687213遇到错误。
只是想知道我是否可以得到有关调试错误的帮助。我知道数组可以很好地复制,因为我可以输出它。所以我认为如何分配值?感谢您能提供的任何帮助。我尝试了几种不同的方法,似乎无法使它正常工作?
您应该尝试
mazeCopy = Array.new(MAZE_SIZE){Array.new(MAZE_SIZE)}
for i in (0...MAZE_SIZE)
for j in (0...MAZE_SIZE)
mazeCopy[i][j] = $maze[j][i]
end
end
# using triple dots to exclude the last value in the range
# error is occurring because you're trying to access value
# outside the range of your array which gives nil.
请参见此示例,为什么会发生此错误:
a = [[1,2,3], [1,2,3], [1,2,3]]
b = a
for i in (0..a.size)
for j in (0..a.size)
# when value of i or j reaches 3
# b[3] => nil
# b[3][] => [] on nil class error
b[i][j] = a[j][i]
end
end