Skip to content

Instantly share code, notes, and snippets.

@aeg
Last active December 16, 2015 15:09
Show Gist options
  • Save aeg/5454280 to your computer and use it in GitHub Desktop.
Save aeg/5454280 to your computer and use it in GitHub Desktop.
リストを空にする
package org.eiji.list
// Case #1
// リストを空にする
def list1 = ['a', 'b', 'c']
list1.clear()
assert list1 == []
// Case #2
// リストを空にする
list1 = ['a', 'b', 'c']
list1 = []
assert list1 == []
// どちらでも同じように見える
// Case #3
// リストを空にするやりかたの比較
list1 = ['a', 'b', 'c']
// List.clear() は、listを実現するために使われる領域のメモリは解放しない
assert list1.elementData == ['a', 'b', 'c']
list1.clear()
assert list1.size() == 0
assert list1.elementData.length == 3
// list = [] はメモリ領域を解放する
list1 = []
assert list1.size() == 0
assert list1.elementData.length == 0
// ちなみに、ArrayList() は指定しないと初期値で10個分の領域を確保する
list2 = new ArrayList()
assert list2.size() == 0
assert list2.elementData.length == 10
// Thanks to this page http://stackoverflow.com/questions/6961356/list-clear-vs-list-new-arraylistinteger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment