الفرق بين المراجعتين ل"Kotlin/collections/indices"

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
(مراجعة + إضافة مثالين)
ط
 
(مراجعة متوسطة واحدة بواسطة نفس المستخدم غير معروضة)
سطر 1: سطر 1:
<noinclude>{{DISPLAYTITLE: الخاصية indices في لغة Kotlin}}</noinclude>
+
<noinclude>{{DISPLAYTITLE: الخاصية <code>‎‎indices</code> في Kotlin}}</noinclude>
 
تُستخدَم هذه الخاصيّة للحصول على الفهارس الصحيحة (valid indices) للمصفوفة أو المجموعة (collection).
 
تُستخدَم هذه الخاصيّة للحصول على الفهارس الصحيحة (valid indices) للمصفوفة أو المجموعة (collection).
  
سطر 21: سطر 21:
  
 
== أمثلة ==
 
== أمثلة ==
 
+
استخدام الخاصية <code>indices‎</code> مع المصفوفات:<syntaxhighlight lang="kotlin">
=== استخدام الخاصية <code>indices‎</code> مع المصفوفات (Arrays) ===
 
تُنشِئ الشيفرة الآتية مصفوفةً من أربعة أعدادٍ صحيحةٍ بالاعتماد على الدالة <code>arrayOf</code>، ثم تستدعي الخاصيّة <code>indices‎</code> لهذه المصفوفة لطباعة فهارسها (indices):<syntaxhighlight lang="kotlin">
 
 
fun main(args: Array<String>) {
 
fun main(args: Array<String>) {
 
     val array = arrayOf(4,5,7,9)
 
     val array = arrayOf(4,5,7,9)
 
     println(array.indices) // 0..3
 
     println(array.indices) // 0..3
 
}
 
}
</syntaxhighlight>
+
</syntaxhighlight>استخدام الخاصية <code>indices‎</code> مع مصفوفة فارغة:<syntaxhighlight lang="kotlin">
 
 
=== استخدام الخاصية <code>indices‎</code> مع مصفوفة فارغة (Empty Array) ===
 
تُنشِئ الشيفرة الآتية مصفوفةً فارغةً باسم <code>empty</code> بالاعتماد على الدالة <code>arrayOf</code>، ثم تستدعي الخاصيّة <code>indices‎</code> لهذه المصفوفة لطباعة فهارسها (indices)، وستظهر النتيجة <code>‎0..-1</code> لأنَّ المصفوفة فارغة:<syntaxhighlight lang="kotlin">
 
 
fun main(args: Array<String>) {
 
fun main(args: Array<String>) {
 
     val empty: Array<Int> = arrayOf()
 
     val empty: Array<Int> = arrayOf()
 
     println(empty.indices) // 0..-1
 
     println(empty.indices) // 0..-1
 
}
 
}
</syntaxhighlight>
+
</syntaxhighlight>استخدام الخاصية <code>indices‎</code> مع المجموعات (Collections):<syntaxhighlight lang="kotlin">
 
 
=== استخدام الخاصية <code>indices‎</code> مع المجموعات (Collections) ===
 
تُنشِئ الشيفرة الآتية مجموعةً تتألف من ثلاثة محارف (characters) بالاعتماد على الدالة <code>listOf</code>، ثم تستدعي الخاصيّة <code>indices</code> لهذه المجموعة لطباعة فهارسها (indices):<syntaxhighlight lang="kotlin">
 
 
fun main(args: Array<String>) {
 
fun main(args: Array<String>) {
 
     val collection = listOf('a', 'b', 'c')
 
     val collection = listOf('a', 'b', 'c')
سطر 48: سطر 40:
  
 
== انظر أيضًا ==
 
== انظر أيضًا ==
* الخاصيّة <code>[[Kotlin/Array/size|size]]</code>: تعبِّر عن عدد العناصر الموجودة في المصفوفة.
+
* الخاصيّة <code>[[Kotlin/Array/size|size]]</code>: تمثل عن عدد العناصر الموجودة في المصفوفة.
  
* الخاصيّة <code>[[Kotlin/collections/lastIndex|lastIndex]]</code>: تعيد فهرس العنصر الأخير في المصفوفة.
+
* الخاصيّة <code>[[Kotlin/collections/lastIndex|lastIndex]]</code>: تمثل فهرس العنصر الأخير في المصفوفة.
  
 
== مصادر ==
 
== مصادر ==
 
* [https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/indices.html صفحة الخاصيّة indices في التوثيق الرسميّ للمكتبة القياسيّة في لغة Kotlin.]
 
* [https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/indices.html صفحة الخاصيّة indices في التوثيق الرسميّ للمكتبة القياسيّة في لغة Kotlin.]
 
[[تصنيف:Kotlin]]
 
[[تصنيف:Kotlin]]
[[تصنيف:Kotlin Function]]
+
[[تصنيف:Kotlin Property]]
 +
[[تصنيف:Kotlin collection]]

المراجعة الحالية بتاريخ 06:48، 8 سبتمبر 2018

تُستخدَم هذه الخاصيّة للحصول على الفهارس الصحيحة (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: تمثل فهرس العنصر الأخير في المصفوفة.

مصادر