반응형
1. List
- get(), contains() 등의 함수가 쓰임(수정 불가능)
- MutableList, ArrayList, LinkedList 모두 List를 상속하여 만들어 진다.
- List는 수정 불가능한 List와 수정가능한 MutableList로 나뉜다
val list: List<Int> = listOf(1,2,3)
val list2 = listOf(1,"d",11L) //List<Any>로 자동타입추론
list[0] = 2 //수정 불가능하기 때문에 Error 발생
var result = list.get(0)
2. Array
- 정적 할당 → 선언 시 크기를 지정하거나 초기화를 진행해야 함.(수정 가능)
- element의 추가, 삭제는 불가능
val array: Array<Int> = arrayOf(1,2,3)
val array2 = arrayOf(1,"d",3.4f) //Array<Any>로 자동타입추론
array[0] = 3
array.set(1, 5) //set(index, value)
array[1] //5
array.get(0) //3
3. MutableList
- add(), remove(), set(), clear() 등의 함수가 쓰인다(수정 가능)
- 추가, 삭제, 수정이 가능하다.
- List를 상속하여 만들어 진다.
- Kotlin에만 존재한다.
- 일반적으로 데이터의 형변환을 사용해야 할 경우에 Mutable을 사용(val, var의 사용)
val arrayList = arrayListOf<Int>()
arrayList.add(10)
arrayList.add(20)
//arrayList의 element를 추가하더라도 주소가 변경되는 것이 아니기 때문에 val을 써도 element 추가가능
arrayList = arrayListOf() //val cannot be reassigned
4. ArrayList
- MutableList를 상속하여 만들어 진다.
- MutableList와 같이 추가, 삭제, 수정이 가능하다.
- 대부분의 RecyclerView는 ArrayList로 만들어 진다.
class ArrayList<E> : MutableList<E>, RandomAccess
5. LinkedList
- Data와 Link를 가지는 Node라는 것으로 이루어진 리스트
- 첫번째나 마지막 원소가 추가 / 삭제 / 수정 등이 빈번하게 일어나는 리스트에서 사용한다.
Reference
반응형
'Android > Android Java-Kotlin' 카테고리의 다른 글
[Android Java] 비동기 Handler, Timer, Coroutine, RxJava, RxKotlin 특징 및 차이 (0) | 2023.12.18 |
---|---|
[Android Java Kotlin] companion object와 static (0) | 2023.10.27 |
[Android Java Kotlin] equals와 ==의 차이(===) (0) | 2023.10.26 |
[Android Java Kotlin] View 숨기기 (0) | 2023.10.23 |