티스토리 뷰

728x90
반응형

배열에서 초기화



val arr = arrayOfNulls<Int>(size)

for (i in arr)

i = sc.nextInt()



Val cannot be reassigned


에러가 뜬다



이유는 위 코드는 자바에서


for (final int i : array ) { .. }



와 같기 때문이란다




그래서 이와 같이 바꾸어준다



for (i in arr.indicies)

arr[i] = sc.nextInt()




+Reference


https://stackoverflow.com/questions/50466863/kotlin-val-cannot-be-reassigned-but-can-be-reassigned-by-accessing-from-inde >

반응형

'프로그래밍 > Kotlin' 카테고리의 다른 글

[Kotlin] Kotlin 의 특수기호  (0) 2018.08.25
[Kotlin] lateinit vs lazy  (0) 2018.08.23
[Kotlin] Convert a char to int  (0) 2018.08.10
[Kotlin] Collections 초기화  (0) 2018.08.10
[Kotlin] Int[] n = new Int[size]  (0) 2018.08.09
공지사항
최근에 올라온 글