[Kotlin] for 루프에서 리스트에 원소 추가, 루프 끝나고 원소 하나 더 추가 => 오류 발생

forEachIndexed 블록 안의 for 루프 안에서 리스트에 원소를 추가하였다. 이 루프가 끝나고 그 리스트에 원소를 하나 더 추가하자 오류가 발생했다( java.lang.IndexOutOfBoundsException). if 문을 써서 문제를 해결했다. for 루프 밖에서 원소를 하나 더 추가하자 오류 발생 val numbers: MutableList<MutableList<Int>> // numbers 는 이미 다른 곳에서 초기화 되어 있다. val intervals: MutableList<MutableList<Int>> = mutableListOf() for (i in … 더 읽기

[Kotlin][deep copy] 리스트의 마지막 원소가 사라져 버렸다.

Kotlin 코드에서 deep copy 를 하지 않은 채 원본을 clear 하니까 리스트의 마지막 원소가 사라지는 문제가 발생했다. 리스트의 마지막 원소만 사라진다. val testList: MutableList<Int> val listOfTestList: MutableList<List<Int>> .forEach 블록 내에서 testList 를 몇 개 생성하면서 listOfTestList 에 추가하였다: listOfTestList.add(testList) 나중에 listOfTestList 를 출력해 보니 감쪽같이 마지막 원소만 사라졌다. 마지막 element 가 [] 이렇게 빈 채로 … 더 읽기

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny