Kotlin - Day 01
1. Reespository fork & clone
1) fork 하고자 하는 Repository에 들어간다.
2) 해당 Repostiory 페이지 오른쪽 상단에 있는 Fork를 클릭한다.
3) clone 하고자 하는 local 환경의 폴더를 지정하여 git bash를 실행시킨다.
4) 다음 명령어 입력을 통해 clone을 해준다.
git clone https://github.com/{본인 GitHub Username}/{저장소이름} -b {Branch Name} --single-branch
2. Intellij Project 생성
1) clone한 폴더 내에 빈 폴더를 생성한다.
2) 생성된 빈 폴더를 Project Location으로 지정하여 새로운 프로젝트를 생성한다.
3) src > main > kotlin으로 경로 이동하여 kotlin 파일을 만든 후, 코드를 작성한다.
3. Input String index slicing
var str = readLine()!!.toString()
// str의 index 1부터 3까지의 값을 출력한다.
println(str.slice(1..3))
※ Substring - slicing과 비슷한 역할의 함수이다.
var str = readLine()!!.toString()
// str의 index 1부터 3까지의 값을 출력한다.
println(str.substring(1, 3))
4. String to List<Int>
val str = "1,2,3"
val listA: List<Int> = paramA.split(',').map {it.toInt()} // [1,2,3]
<참조>
https://stackoverflow.com/questions/65550796/convert-string-to-integer-list-in-kotlin
Convert String to Integer List in Kotlin
I want to make function for typeconverter room. The original data is in List and i store it as String in database. My code and the output is like this: fun main() { val genreIds = listOf(28, 80...
stackoverflow.com
5. List vs Array
1) List
장점
- Size가 고정되어 있지 않아 flexible하게 사용할 수 있다.
- item 추가, 제거에 대한 성능이 뛰어나다.
단점
- Index 접근을 하기 위해선 List 전체를 순회해야 한다.
2) Array
장점
- Index 접근에 용이하다.
단점
- Size가 고정되어 있다.
- 앞에 있는 item을 제거하면 뒤에 있는 item들을 모두 앞으로 끌어와야 하기 때문에 List에 비해 성능이 뒤쳐진다.
6. Mutable List vs Immutable List
1) Immutable List (List)
- size 고정 (add, remove 불가능)
val immutableList = listOf(1, 2, 3) // 초기화
val otherList: List<Int> = immutableList // 리스트 타입 선언
2) Mutable List
- add, remove 가능
val mutableList = mutableListOf(1, 2, 3) // 초기화
val otherList: MutableList<Int> = mutableList // 리스트 타입 선언
※ Immutable List -> Mutable List
val immutableList = listOf(1, 2, 3) // 초기화
val mutableList: MutableList<Int> = immutableList.toMutableList() // Immutable -> Mutable
※ Mutable List -> Immutable List
val mutableList = mutableListOf(4, 5, 2, 1, 3) // 초기화
val immutableList = mutableList.toList() // Mutable -> Immutable
※ 정렬
val mutableList = mutableListOf<Int>(4, 5, 2, 1, 3) // 초기화
val immutableList = mutableList.sorted() // not in-place && return type is List<T>
mutableList.sort() // in-place
<참조>
https://kumgo1d.tistory.com/51
[Kotlin] Collections List 사용법
안녕하세요 골드입니다. 오늘은 코틀린의 컬렉션 중에서 List에 대해 글을 쓰도록 하겠습니다. 1. List의 기본 형태 먼저 List의 가장 기본적인 모습입니다. List는 순서가 있는 데이터의 집합입니
kumgo1d.tistory.com
7. List item 중복 제거
- Mutable List에는 사용 불가능... (List로 변환 후 사용해야 한다.)
var immutableList = listOf(4, 5, 2, 1, 3, 2, 2, 2) // 초기화
immutableList = immutableList.distinct() // return type is List<T>
8. Check weather a List contains specific item or not
- Immutable, Mutable 상관없이 사용 가능하다.
val immutableList = listOf(4, 5, 2, 1, 3, 2, 2, 2) // 초기화
if (immutableList.contains(3)){
println("Contain")
}
<참조>
https://stackoverflow.com/questions/42234775/kotlin-idiomatic-way-to-check-array-contains-value
Kotlin - Idiomatic way to check array contains value
What's an idiomatic way to check if an array of strings contains a value in Kotlin? Just like ruby's #include?. I though about: array.filter { it == "value" }.any() Is there a better way?
stackoverflow.com
9. gitignore 추가
build
.gradle
.idea
*.iml
10. String -> Integer
var str = "123" // String
var num = Integer.parseInt(str) // Integer
<참조>
https://wdprogrammer.tistory.com/11
[Kotlin] 코틀린에서 형변환은 어떻게 할까
2018-10-17-kotlin-casting Java에서는 자동 형변환을 지원했으며, 강제 형변환도 손쉽게 가능했다. 형변환이 가능하다면 그냥 변수명 앞에 (타입)만 붙여주면 된다. 그렇지만 코틀린은 자동 형변환을 지
wdprogrammer.tistory.com
<정리>
1. listOf와 List 구분하기
listOf : 변수 초기화에 쓰인다.
List : 변수 Type 선언시에 쓰인다.
2. sort() 와 sorted() 구분하기
sort() : In-place 함수로 MutableList Type에 쓰인다.
sorted() : not In-place 함수로 List Type을 반환한다.
3. 내장함수 contains, indexOf 활용하기
{listName}.contains(element) : return True or False
{listName}.indexOf(element) : return index (해당하는 element가 없으면 -1을 반환)
4. 내장함수 remove, removeAt 활용하기
{listName}.remove(element) : 해당 element 제거 (동일한 element가 존재하면, 맨 앞에 있는 element를 제거한다.)
{listName}.removeAt(index) : 해당 index의 element를 제거
5. Generic
-> 추가적으로 작성할 계획.