Kotlin IN ACTION - Chapter7 #6
hyunjung-choi
started this conversation in
General
Replies: 1 comment 1 reply
-
data class Code(val year: String, val text: String) : Comparable<Code> {
override fun compareTo(other: Code): Int {
return if (year != other.year) year.toInt() - other.year.toInt()
else text.compareTo(other.text)
}
}
private fun printCodeList(codes: List<Code>) {
for (code in codes) {
println("${code.year}-${code.text}")
}
}
private fun solution(codes: List<String>) {
val codeList = mutableListOf<Code>()
for (code in codes) {
val (year, text) = code.split("-")
codeList.add(Code(year, text))
}
codeList.sort()
printCodeList(codeList)
}
fun main() {
val codes = listOf(
"20-DE0815",
"20-CO1299",
"20-MO0901",
"20-KE0511",
"20-SP1102",
"21-DE0401",
"21-CO0404",
"21-MO0794",
"21-KE0704",
"21-SP0404",
"19-DE0401",
"19-CO0404",
"19-MO0794",
"19-KE1204",
"19-SP0404"
)
solution(codes)
}
operator fun Char.times(count: Int) : String {
return toString().repeat(count)
}
fun main() {
println('a' * 3) // aaa
} 이 연산자는 이런식의 결과 타입 조합도 합법적인 연산자 오버로딩이다.
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
ArrayList
클래스를 상속한MyList
의 모든 원소를 1씩 증가하는 함수를 만들고자 한다. 이때 관례를 이용해 구현해보자!(참고: https://toss.im/career/article/next-developer-2023-sample-questions)
Beta Was this translation helpful? Give feedback.
All reactions