Notice
Recent Posts
Recent Comments
올해는 머신러닝이다.
list(추가 불가능함)에 add 를 달아보자 본문
fun appendList<T>(listOfLists: List<List<T>>, toAppend: List<T>): List<List<T>> {
}
Of course both of the following work:
fun appendList<T>(listOfLists: List<List<T>>, toAppend: List<T>): List<List<T>> {
// Use spread operator
return listOf(*listOfLists.toTypedArray(), toAppend);
}
fun appendList<T>(listOfLists: List<List<T>>, toAppend: List<T>): List<List<T>> {
// Turn to mutable list
val ret = listOfLists.toArrayList()
ret.add(toAppend)
return ret
}
But this is confusing to users who might expect + to work. Due to type erasure, + will always have quirks like this. I had to add the following extension:
operator fun <T, C : Collection<T>> List<C>.plus(toAppend: C): List<C> {
val ret = java.util.ArrayList<C>(this.size + 1)
ret.addAll(this)
ret.add(toAppend)
return ret
}
'링크모음 > 코틀린' 카테고리의 다른 글
코틀린에서 list 를 map 으로 빠르게 변경하기 (0) | 2018.01.04 |
---|---|
코틀린 Parcelable 추가 방법 (새롭게 추가된 방법) (0) | 2017.12.28 |
코틀린 키워드 모음 (0) | 2017.12.04 |
Sealed class + when 자세한 설명 (0) | 2017.11.21 |
접근 제어자 관련 링크 (0) | 2017.11.20 |