Created
September 18, 2012 06:48
-
-
Save aeg/3741669 to your computer and use it in GitHub Desktop.
Groovyのリストへの要素追加。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ListPut | |
// Case #1 | |
// リストの最初の要素を取得する。 | |
def list = ['a', 'b', 'c'] | |
assert list.first() == 'a' | |
assert list.head() == 'a' | |
assert list[0] == 'a' | |
assert list == ['a', 'b', 'c'] // リスト自体に変化なし。 | |
// Case #2 | |
// リストの最後の要素を取得する。 | |
list = ['a', 'b', 'c'] | |
assert list.last() == 'c' | |
assert list[-1] == 'c' | |
assert list == ['a', 'b', 'c'] // リスト自体に変化なし。 | |
// Case #3 | |
// リストの最後の要素を取得し、リストから削除する。 | |
list = ['a', 'b', 'c'] | |
assert list.pop() == 'c' | |
assert list == ['a', 'b'] | |
assert list.pop() == 'b' | |
assert list == ['a'] | |
// Case #4 | |
// リストの最初の要素を除いたリストを取得する。 | |
list = ['a', 'b', 'c'] | |
assert list.tail() == ['b', 'c'] | |
assert list == ['a', 'b', 'c'] // リスト自体に変化なし。 | |
// Case #5 | |
// リストの最後の要素を除いたリストを取得する。 | |
list = ['a', 'b', 'c'] | |
assert list[0..-2] == ['a', 'b'] | |
assert list == ['a', 'b', 'c'] // リスト自体に変化なし。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Case #1 | |
// リストの最後に要素を追加する。リストの長さは長くなる。 | |
def list = ['a','b','c'] | |
list.push('x') | |
assert list == ['a', 'b', 'c', 'x'] | |
// Case #2 | |
// リストの最後に要素を追加する。別の記法。 | |
list = ['a','b','c'] | |
list << 'x' | |
assert list == ['a', 'b', 'c', 'x'] | |
// 追加はチェーンさせられる。 | |
list << 'y' << 1 << [2, 3] | |
assert list == ['a', 'b', 'c', 'x', 'y', 1 , [2, 3]] | |
// Case #3 | |
// リストの最後にリストを追加すると。リストが追加される。 | |
list = ['a','b','c'] | |
list.push(['d', 'e']) | |
assert list == ['a', 'b','c' ,['d', 'e']] | |
// Case #4 | |
// リストの任意の位置の要素を置き換える。リストの長さは変わらない。 | |
list = ['a','b','c'] | |
list.putAt(0, 'x') | |
assert list == ['x', 'b', 'c'] | |
// Case #5 | |
// リストの任意の位置にリストの要素を追加する。元のリストは保存される。 | |
list = ['a','b','c'] | |
list.addAll(0, ['d', 'e']) | |
assert list == ['d', 'e', 'a', 'b', 'c'] | |
// Case #6 | |
// リストの任意の位置にリストの要素を追加する。元のリストは保存される。 | |
list = ['a','b','c'] | |
list.addAll(1, ['d', 'e']) | |
assert list == ['a', 'd', 'e', 'b', 'c'] | |
// Case #7 | |
// リストの最後の位置にリストの要素を追加する。元のリストは保存される。 | |
list = ['a','b','c'] | |
list.addAll(['d', 'e']) | |
assert list == ['a', 'b', 'c', 'd', 'e'] | |
// Case #8 | |
// リストの任意の位置の要素をリストに置き換える。リストの長さは変わらない。 | |
list = ['a','b','c'] | |
list.putAt(0, ['d', 'e']) | |
assert list == [['d', 'e'], 'b', 'c'] | |
// Case #9 | |
// リストの任意の位置の要素をリストに置き換える(上と同じ内容の別の書き方)。リストの長さは変わらない。 | |
list = ['a','b','c'] | |
list[0] = ['d', 'e'] | |
assert list == [['d', 'e'], 'b', 'c'] | |
// Case #10 | |
// リストの任意の範囲に要素をリストの要素で置き換える。リストの長さは変わらない。 | |
list = ['a','b','c'] | |
list.putAt(0..1, ['d', 'e']) | |
assert list == ['d', 'e', 'c'] | |
// Case #11 | |
// リストの任意の範囲に要素をリストの要素で置き換える。範囲が0..0の場合にはこのような動きになっている。 | |
list = ['a','b','c'] | |
list.putAt(0..0, ['d', 'e']) | |
assert list == ['d', 'e', 'b', 'c'] | |
// Case #12 | |
// リストの任意の範囲に要素をリストの要素で置き換える。範囲が元のリスト全体の場合には、 | |
// それらを全て置き換えるが、指定した置き換え用のリストは拡張されない。よって、足りない | |
// ところは置き換えられず空になる。 | |
list = ['a','b','c'] | |
list.putAt(0..2, ['d', 'e']) | |
assert list == ['d', 'e'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment