Kotlin147 [Kotlin][String] replace 모든 oldChar 가 newChar 로 대체된 새 문자열을 반환합니다.public actual fun String.replace(oldValue: String, newValue: String, ignoreCase: Boolean = false): String { run { var occurrenceIndex: Int = indexOf(oldValue, 0, ignoreCase) // FAST PATH: no match if (occurrenceIndex = length) break occurrenceIndex = indexOf(oldValue, occurrenceIndex + searchStep, ignoreCase) } whi.. 2024. 8. 27. [Kotlin][Iteration Operation] repeat 주어진 함수 작업을 지정된 횟수만큼 실행합니다. 현재 반복의 0부터 시작하는 인덱스가 작업에 매개변수로 전달됩니다.public inline fun repeat(times: Int, action: (Int) -> Unit) { contract { callsInPlace(action) } for (index in 0 until times) { action(index) }}repeat(3) { index -> print("Hello ")} // Hello Hello Hello n 번 반복된 이 문자 시퀀스를 포함하는 문자열을 반환합니다.public actual fun CharSequence.repeat(n: Int): String { require(n >= 0) { "Coun.. 2024. 8. 27. [Kotlin][Collection] sortedBy / sortedByDescending 지정된 선택기 함수에 의해 반환된 값의 자연 정렬 순서에 따라 정렬된 모든 요소의 목록을 반환합니다. 정렬이 안정적입니다. 이는 동일한 요소가 정렬 후에도 서로 상대적인 순서를 유지함을 의미합니다.public inline fun > Iterable.sortedBy(crossinline selector: (T) -> R?): List { return sortedWith(compareBy(selector))}public fun Iterable.sortedWith(comparator: Comparator): List { if (this is Collection) { if (size () as Array).apply { sortWith(comparator) }.asList() } .. 2024. 8. 23. [Kotlin][Collection] sorted / sortedDescending 기본 정렬 순서에 따라 정렬된 모든 요소의 목록을 반환합니다. 정렬이 안정적입니다. 이는 동일한 요소가 정렬 후에도 서로 상대적인 순서를 유지함을 의미합니다.public fun > Iterable.sorted(): List { if (this is Collection) { if (size >() as Array).apply { sort() }.asList() } return toMutableList().apply { sort() }}val list = listOf(4, 3, 2, 1)print(list.sorted()) // [1, 2, 3, 4] 자연 정렬 순서에 따라 내림차순으로 정렬된 모든 요소의 목록을 반환합니다. 정렬이 안정적입니다. 이는 동일한 요소가 정렬 후에도 .. 2024. 8. 23. [Kotlin][Collection] reversed 요소가 역순으로 포함된 목록을 반환합니다.public fun Iterable.reversed(): List { if (this is Collection && size reversed - Kotlin Programming Language kotlinlang.org 2024. 8. 23. [Algorithm][Kotlin] 정렬 알고리즘 (선택, 삽입, 버블, 퀵, 병합) 정렬 (sorting)어떤 데이터들이 주어졌을 때 이를 정해진 순서대로 나열하는 것val dataList = (0..99).shuffled().take(10).toIntArray()println("Before sorting: ${dataList.joinToString(", ")}") 선택 정렬 (Selection Sort)1. 주어진 리스트 중에 최소값을 찾는다.2. 해당 최소값을 데이터 맨 앞에 위치한 값과 교체한다.3. 맨 처음 위치를 뺀 나머지 데이터를 같은 방법으로 교체한다.시간 복잡도average, worst : O(N²) (구현이 간단하지만 비효율적)공간 복잡도제자리(In-place) 알고리즘 (기존 배열 외에 추가적인 메모리를 거의 사용하지 않는다)안전성불안전정렬 (정렬을 수행할 때 동일한 .. 2024. 8. 22. [Kotlin][Collection] unzip 목록 쌍을 반환합니다. 여기서 첫 번째 목록은 이 컬렉션의 각 쌍의 첫 번째 값에서 작성되고, 두 번째 목록은 이 컬렉션의 각 쌍의 두 번째 값에서 작성됩니다.public fun Iterable>.unzip(): Pair, List> { val expectedSize = collectionSizeOrDefault(10) val listT = ArrayList(expectedSize) val listR = ArrayList(expectedSize) for (pair in this) { listT.add(pair.first) listR.add(pair.second) } return listT to listR}val list = listOf(1 to .. 2024. 8. 22. 이전 1 ··· 11 12 13 14 15 16 17 ··· 21 다음