14
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

簡単な二次元配列の作り方

Last updated at Posted at 2015-10-28

ググってもなかなかピンとくる書き方が無かったため、メモ。
やっていることは、一次元配列に一次元配列を格納してるだけ。

arr = []
#適当に3回ループ
3.times do
	val1 = "a"
	val2 = "b"
	arr << [val1, val2]
end

配列の中身

[["a","b"],["a","b"],["a","b"]]

これで、String[2,3]の二次元配列ができる。

取り出すときは、

arr.each do | val1, val2 |
	p val1
	p val2
end

みたいに、doの後ろの||の中を増やしてあげれば簡単に取り出せる。

Rubyの多次元配列初めて使ったけど、他の言語に比べて扱いが難しい気がする。

14
13
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
13

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?