Kotlin - 배열객체의 멤버 호출

CODEDRAGON Development/Kotlin

반응형


 

 

배열객체의 멤버 호출

연산자 함수와 프로퍼티를 통해 요소값의 참조 설정이 가능합니다.

 

 

get()

특정 인덱스의 특정 값을 리턴합니다.

 

operator fun get(index: Int): T

 

get() 인덱스 연산자 [] 호출할 있습니다.

배열명.get(인덱스)

obj.get(1)

obj[1]

 

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-array/get.html

 

 

 

first()

해당 배열의 첫번째 요소를 반환합니다.

배열명.first()

 

 

 

last()

해당 배열의 마지막 요소를 반환합니다.

배열명.last()

 

 

 

set()

특정 인덱스의 특정 값을 설정합니다.

 

 

operator fun set(index: Int, value: T)

 

 

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-array/set.html

 

 

set() 인덱스 연산자 [] 호출할 있음

배열명.get(인덱스, )

obj.set(1, 1004)

obj[1] = 1004

 

 

 

 

joinToString()

배열의 요소를 해당 구분자로 나눠진 문자열로 반환해 줍니다.

fun <T> Array<out T>.joinToString(

    separator: CharSequence = ", "

    prefix: CharSequence = ""

    postfix: CharSequence = ""

    limit: Int = -1

    truncated: CharSequence = "..."

    transform: (T) -> CharSequence = null

): String

 

 

http://bit.ly/2OBrw93

 

 

 

 

fill()

·       해당 element 요소를 fromIndex 부터 toIndex까지 채웁니다.

·       toIndex 요소는 포함되지 않습니다.

 

fun <T> MutableList<T>.fill(value: T)

 

 

http://bit.ly/2S7K7rr

 

 

 

 

 

.size

Java에서는 .length 사용했지만

Kotlin에서는 .size 사용하여 해당 배열의 크기를 반환합니다.

println(obj.size)

 

 

 

 

.lastIndex

배열의 마지막요소의 인덱스를 반환합니다.

println(obj.lastIndex)

 

 

 

 

 

.indices

해당 배열의 인덱스 범위를 range 반환합니다.

/**

 * Returns the range of valid indices for the array.

 */

public val <T> Array<out T>.indices: IntRange

    get() = IntRange(0, lastIndex)

 

 

println(array.indices)

0..3

 

 

 

 


반응형

'Development > Kotlin' 카테고리의 다른 글

init block  (0) 2019.02.23
Any 타입, kotlin.Any  (0) 2019.02.18
Kotlin - Loop control  (0) 2019.02.07
Kotlin - 프로퍼티(Property)  (0) 2019.02.01
Kotlin - 내부 클래스(Inner Class)의 구성 형식  (0) 2019.01.27