Skip to content

Instantly share code, notes, and snippets.

@aya-eiya
Created October 15, 2012 05:11
Show Gist options
  • Save aya-eiya/3890894 to your computer and use it in GitHub Desktop.
Save aya-eiya/3890894 to your computer and use it in GitHub Desktop.
気になったらすぐテストしよう ref: http://qiita.com/items/7ddb52be1ad0cd67e36f
// はたと困ったのでテスト
// T[] arr=ArrayList<T>.toArray(T[])
// で得られた配列arrをいじくるとどうなるんだったっけ?
// 予想では引数として与えた配列へ値がコピーされ、
// 返却値は配列への参照となっているハズ。
ArrayList<String> alist = new ArrayList<String>()
alist.add(0,'文字列1')
alist.add(0,'文字列2')
alist.add(0,'文字列3')
assert(alist.get(0)=='文字列3')
assert(alist.get(1)=='文字列2')
assert(alist.get(2)=='文字列1')
String[] strs = alist.toArray(new String[3])
String[] test = ['文字列3','文字列2','文字列1']
assert(strs == test)
// 配列の中身を変えてみる
strs[0] = strs[0].replace(/文字列/,'もじれつ')
strs[1] = strs[1].replace(/文字列/,'もじれつ')
strs[2] = strs[2].replace(/文字列/,'もじれつ')
assert(strs != test) // 配列の中身が変わった
String[] strs2 = alist.toArray(new String[3])
assert(alist.get(0)=='文字列3')
assert(alist.get(1)=='文字列2')
assert(alist.get(2)=='文字列1')
assert(strs != strs2) // alistの中身の変更はなし
// 引数にtestを与えてみる
test = ['文字列3','文字列2','文字列1']
strs = alist.toArray(test)
assert(strs[0]=='文字列3')
assert(strs[1]=='文字列2')
assert(strs[2]=='文字列1')
assert(strs == test)
// 配列の中身を変えてみる
strs[0] = strs[0].replace(/文字列/,'もじれつ')
strs[1] = strs[1].replace(/文字列/,'もじれつ')
strs[2] = strs[2].replace(/文字列/,'もじれつ')
assert(strs == test) // strsとtestが同時に変更されている。
println strs
println test
assert(strs.is(test)) // 参照が一致している。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment