الخاصية indices
في Kotlin
< Kotlin | collections
اذهب إلى التنقل
اذهب إلى البحث
تُستخدَم هذه الخاصيّة للحصول على الفهارس الصحيحة (valid indices) للمصفوفة أو المجموعة (collection).
البنية العامة
توفّر لغة Kotlin الخاصيّة indices للمصفوفات من الأنواع المختلفة كما يلي:
val <T> Array<out T>.indices: IntRange
val ByteArray.indices: IntRange
val ShortArray.indices: IntRange
val IntArray.indices: IntRange
val LongArray.indices: IntRange
val FloatArray.indices: IntRange
val DoubleArray.indices: IntRange
val BooleanArray.indices: IntRange
val CharArray.indices: IntRange
وكذلك للمجموعة (collection)
val Collection<*>.indices: IntRange
القيم المعادة
الفهارس الصحيحة (valid indices) للمصفوفة أو المجموعة (collection).
أمثلة
استخدام الخاصية indices
مع المصفوفات:
fun main(args: Array<String>) {
val array = arrayOf(4,5,7,9)
println(array.indices) // 0..3
}
استخدام الخاصية indices
مع مصفوفة فارغة:
fun main(args: Array<String>) {
val empty: Array<Int> = arrayOf()
println(empty.indices) // 0..-1
}
استخدام الخاصية indices
مع المجموعات (Collections):
fun main(args: Array<String>) {
val collection = listOf('a', 'b', 'c')
println(collection.indices) // 0..2
}
انظر أيضًا
- الخاصيّة
size
: تمثل عن عدد العناصر الموجودة في المصفوفة.
- الخاصيّة
lastIndex
: تمثل فهرس العنصر الأخير في المصفوفة.