list.forEach{print("$it")} // 출력 1 2 3 4 5 6 list.forEachIndexed{index, value -> println("index[$index]: $value")} list.onEach{print(it)} // 출력123456 각 요소를 람다식으로 처리 후 컬렉션으로 반환 forEach 잘 쓰면 좋아 보인다. for문을 사용하지 않아도 되네 forEachIndexed는 인덱스랑 value를 받을 수 있다. onEach 도 있네 차이점에 대해 공부해 보자 forEach랑 무슨 차이일까 forEach -> 각 요소를 람다식으로 처리 // onEach -> 각 요소를 람다식으로 처리 후 컬렉션으로 반환 println(list.count{it % == 2}) //와 이게..
프로그래밍 2021. 4. 8. 21:25
arr3 = [i*2 for i in range(0,5)] 위와 같은 식을 코틀린에서도 가능하다 val arr3 = Array(5, {i -> i*2}) println("arr3: ${Arrays.toString(arr3)}") 을 하면된다. 물론 import java.util.Arrays 를 해줘야한다. 배열이 정의되면 고정하기 때문에 plus로 새 요소 추가 안됨 sliceArray로도 잘라내어도 기존 리스트에 할당 못 한다. 자료형 지정된 배열은 다른 자료형 변환x 대신 Any 자료형으로하면 변경 + 여러 자료형 넣기 가능!
프로그래밍 2021. 4. 8. 16:09
위 것들은 공부해야지
카테고리 없음 2021. 4. 8. 09:40
공변성, 반공변성, 무변성 글로만 읽으면 어떤 의미 인지 몰랐는데 좀 이해가 간다. 일단 클래스 관계를 먼저 보면 Nothing -> Int -> Any 이렇게로 오른쪽으로 갈 수록 다 상위 클래스다. 먼저 무변성 class Box(val size :Int) fun main(){ val anys:Box = Box(10) // 오류 자료형 불일치 val nothings:Box = Box(10) // 오류 자료형 불일치 } out 과 in이 없는 거다 위처럼 아에 관계가 없는 무변성 공변성은 class Box(val size :Int) fun main(){ val anys:Box = Box(10) // 관계 성립으로 객체 생성 가능 val nothings:Box = Box(10) // 오류 자료형 불일치 }..
카테고리 없음 2021. 4. 7. 23:45
fun add(a:T, B:T, op: (T,T) -> T) : T{ return op(a,b) } fun add(a:T, b:T) : T{ return a + b //오류! 자료형이 뭔지 몰라서 안됨 } 근데 함수 앞에 나오는 저 의 정체는 대체 뭘까
카테고리 없음 2021. 4. 7. 22:48
package com.example.complie typealias Username = String fun main() { val output = Outer.Nested.grer() //내부 클래스는 객체 생성 없이 사용 가능 //Outer.outside() Unit){ out(a,b) } class Outer{ val ov = 5 class Nested{ val nv = 10 // out 변수인 ov는 불가 fun grer() = println("Hello") som() // companion object는 사용가능 } companion object{ const val country = "Korea" fun som = println("Simple") } fun outside() = Nested().gr..
프로그래밍 2021. 4. 6. 20:15
medium.com/til-kotlin-ko/kotlin%EC%9D%98-%ED%81%B4%EB%9E%98%EC%8A%A4-%EC%9C%84%EC%9E%84%EC%9D%80-%EC%96%B4%EB%96%BB%EA%B2%8C-%EB%8F%99%EC%9E%91%ED%95%98%EB%8A%94%EA%B0%80-c14dcbbb08ad Kotlin의 클래스 위임은 어떻게 동작하는가 If you wanna read English version, please see here. medium.com 너무나 잘 설명해주셨다. medium.com/@lunay0ung/kotlin-object-declaration-%EA%B7%B8%EB%A6%AC%EA%B3%A0-companion-object-feat-static-d5c97c..
프로그래밍 2021. 4. 6. 13:17
class Person{ var id : Int = 0 companion object{ var language : String = "Korean" fun work(){ println("Working") } } } fun main(){ println(Person.language) Person.language = "English" Person.work println(Person.language) //println(Person.id) companion이 아니니 오류! } 자바의 static 처럼 사용할 수 있다. 더 자세한 설명은 아래 링크에서 볼 수 있다. Person 객체를 생성하지 않아도 접근 가능하다 companion을 사용하면! www.bsidesoft.com/8187 [kotlin] Companio..
카테고리 없음 2021. 4. 2. 12:19