-
[Kotlin]무작정 따라하기2Android(+ Kotlin) 2019. 11. 8. 18:50
무작정 따라 하기 1
Kotlin 무작정 따라하기 1
https://codelabs.developers.google.com/codelabs/kotlin-bootcamp-basics/#1 Kotlin Bootcamp for Programmers 2: Kotlin basics In this task, you learn about booleans and checking conditions in the Kotli..
charko.tistory.com
https://codelabs.developers.google.com/codelabs/kotlin-bootcamp-basics/#4
Kotlin Bootcamp for Programmers 2: Kotlin basics
In this task, you learn about booleans and checking conditions in the Kotlin programming language. Like other languages, Kotlin has booleans and boolean operators such as less than, equal to, greater than, and so on (<, ==, >, !=, <=, >=). Write an if/else
codelabs.developers.google.com
부트캠프 사이트 기준으로 진행합니다.
*List
listOf : 변경은 불가 리스트
val school = listOf("mackerel", "trout", "halibut") print(school) // [mackerel, trout, halibut]
mutableListOf : 변경 가능 리스트
val myList = mutableListOf("tuna", "salmon", "shark") print(myList) // [tuna, salmon, shark] myList.remove("shark") // res2: kotlin.Boolean = true print(myList) // [tuna, salmon] val test = listOf(1, "1", 1.1f, 100_000_000)
* val로 선언된 변수는 불변해야 한다. 하지만 리스트는 수정이 가능했다. 왜 수정이 가능할까?
- 코틀린 언어 선언 시 call by value를 하지 않고, call by reference를 기준으로 한다.(java도 동일하다.)
변수를 선언할때 할당되는 주소 값에 값이 들어가는데,
위 뮤테이블리스트는 할당되는 주소 값에 생성되는 리스트의 주소 값이 들어가게 된다. 그래서 리스트 내부의 값이 변경이 되더라도 해당 주소 값이 달라지지 않기 때문에 변경이 가능하다.
* list는 다른 변수 타입을 여러 개 선언할 수 있다.(python tuple 같은...)
* Array
배열은 리스트와 다르게 mutable, immutable버전이 따로 있지 않고, 변경이 불가능하다. 선언과 동시에 사이즈가 고정되며 추가 및 삭제가 되지 않는다. 변경이 필요할 경우 복사하여 새로운 변수를 만들어야 한다.
val school = arrayOf("shark", "salmon", "minnow") print(school) // [Ljava.lang.String;@d8fc855 // 해당 변수의 값은 배열의 메모리 값이 들어가 있다. print(java.util.Arrays.toString(school)) // [shark, salmon, minnow]
각 타입별 배열 선언 방식이 다르다.
val numbers = intArrayOf(1,2,3) val bytes = byteArrayOf(1,2,3) val floats = floatArrayOf(1.1f, 2.2f, 3.3f) // 기본적으로 double타입을 따르며, flaot형태는 f를 붙여야함 val doubles = doubleArrayOf(1.1, 2.2, 3.3) val longs = longArrayOf(1,2,3)
변형이 필요할 경우 새 상수로 선언한다.
val numbers = intArrayOf(1,2,3) val numbers3 = intArrayOf(4,5,6) val foo2 = numbers3 + numbers print(foo2[5]) // 3
배열 선언에 있어 값이 아닌 코드로 초기화가 가능하다.
val array = Array(5) { it * 2 } print(java.util.Arrays.toString(array)) // [0, 2, 4, 6, 8]
초기화 코드는 중괄호 안에서 진행된다.
it : 0으로 시작하는 배열 인덱스
* Loop
val school = arrayOf("shark", "salmon", "minnow") for (element in school) { print(element + " ") } // shark salmon minnow for ((index, element) in school.withIndex()) { print("Item at $index is $element\n") } // Item at 0 is shark // Item at 1 is salmon // Item at 2 is minnow
for (i in 1..5) print(i) // 1~5까지 // 12345 for(i in 5 downTo 1) print(i) // 1씩 내리기 // 54321 for (i in 3..6 step 2) print(i) // 2씩 증가 // 35 for(i in 'd'..'g') print(i) // char형도 가능 // defg
var bubbles = 0 while (bubbles < 50) { bubbles++ } print("$bubbles bubbles in the water\n") // 50 bubbles in the water do { bubbles-- } while (bubbles > 50) print("$bubbles bubbles in the water\n") // 49 bubbles in the water repeat(2) { print("A fish is swimming") } // A fish is swimmingA fish is swimming
while : 참일 때 반복문 진행 (선 비교, 후 처리)
do~while : 참일때 반복문 진행(후 비교, 선 처리)
repeat : ... 그냥 반복
Kotlin 'Android(+ Kotlin)' 카테고리의 다른 글
[수정] ViewModel의 ViewModelProvider (0) 2019.12.30 MVVM 따라하기 Data Binding, LiveData (1) (0) 2019.12.20 [Kotlin]무작정 따라하기3 (0) 2019.11.11 [Kotlin]무작정 따라하기 1 (0) 2019.10.16 JAVA 용어 정리 (0) 2019.09.20