Skip to content

Instantly share code, notes, and snippets.

@aeg
Last active December 10, 2015 14:08
Show Gist options
  • Save aeg/4445509 to your computer and use it in GitHub Desktop.
Save aeg/4445509 to your computer and use it in GitHub Desktop.
リストの要素を削除する
// Case #1
// リストの任意の要素をインデックス指定で削除する
def list1 = ['a', 'b', 'c', 'd']
list1.remove(2)
assert list1 == ['a', 'b', 'd']
// Case #2
// リストの任意の要素を値を指定して削除する
def list2 = ['a', 'b', 'c', 'b']
// リストで先頭から発見された最初の要素しか削除されない
list2.remove('b')
assert list2 == ['a', 'c', 'b']
// 2回呼び出すことにより削除される
result = list2.remove('b')
assert list2 == ['a', 'c']
// 削除ができた場合には result = true
assert result == true
assert list2 == ['a', 'c']
result = list2.remove('b')
// 削除ができなかった場合には result == false
assert result == false
assert list2 == ['a', 'c']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment