본문 바로가기

728x90

Android/Kotlin

(4)
[BOJ][KOTLIN] 2447 별 찍기 - 10 문제 재귀적인 패턴으로 별을 찍어 보자. N이 3의 거듭제곱(3, 9, 27, ...)이라고 할 때, 크기 N의 패턴은 N×N 정사각형 모양이다. 크기 3의 패턴은 가운데에 공백이 있고, 가운데를 제외한 모든 칸에 별이 하나씩 있는 패턴이다. *** * * *** N이 3보다 클 경우, 크기 N의 패턴은 공백으로 채워진 가운데의 (N/3)×(N/3) 정사각형을 크기 N/3의 패턴으로 둘러싼 형태이다. 예를 들어 크기 27의 패턴은 예제 출력 1과 같다. 입력 첫째 줄에 N이 주어진다. N은 3의 거듭제곱이다. 즉 어떤 정수 k에 대해 N=3k이며, 이때 1 ≤ k < 8이다. 출력 첫째 줄부터 N번째 줄까지 별을 출력한다. 예제 입력1 27 예제 출력1 *************************** ..
안드로이드 스튜디오 Kotlin Memory Leak Warning("StaticFieldLeak") - Android Studio (1.4 이상 plugin 설치) 에서 Kotlin Object Class 에 대해서 field 에 Context 를 선언할 경우 Warning 에러가 발생한다. - Error Message Do not place Android context classes in static fields (static reference to AppContext which has field activityContext pointing to Context); this is a memory leak - 해결 방법 1. @SuppressLint("StaticFieldLeak") Annotation 추가한다. 2. context 를 Object class 에 넘겨주는 대신 Application Con..
Subroutine override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val sum = (0..10).toMutableList().sum() result_text.text = "Result : $sum" // sum() 연산이 끝난 후 결과 return } // Subroutine private fun MutableList.sum(): Int = this.sumBy { it }
MutableList * sum() The examples show you how to use sum() function to: sum of all items in the normal List sum of specific field in List of Objects (in need of map()) sum of specific field of all values in a Map (in need of map()) Why we need map()? map() will create a List of quantity first, then we invoke: List.sum() List We’re gonna use sum() on List and List for example. val nums = listOf(10, 20, 30) p..