-1
我在迭代2d數組時遇到了問題。我想簡單地爲這個數組中的每個索引設置一個起始值0.我可以創建表,但現在我想設置初始值。二維數組中的迭代?
輸入格式如下: R5C4 + 2。 (這讀作爲具有5行和4列的表,其中表中的所有值都是2的值)。
初始表:
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
輸入之後:
2 2 2 2
2 2 2 2
2 2 2 2
2 2 2 2
2 2 2 2
Probem是我甚至不能去設置所有表的索引以0初始值,更不用說更新每個值。
到目前爲止我的代碼:
row = []
sign = ''
val = ''
x.chars.each_slice(2) do |u|
case u[0]
when 'R' then
row << u[1]
when 'C' then
col << u[1]
when '+', '-'
sign, val = u[0], u[1]
else
puts 'Invalid input.'
exit
end
end
p col
p row
puts sign, val
big_row = row.max.to_i
big_col = col.max.to_i
table = Array.new (big_row) { Array.new(big_col) }
require 'narray'
table = NArray[big_row][big_col]
table.each do |(x,y)|
x = 0
y = 0
puts x,y
end
檢查您的其他帖子。我在這裏爲你解決了這個問題:http://stackoverflow.com/a/21818644/1550111 @ user3315041 – franksort