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

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
(أنشأ الصفحة ب'<noinclude>{{DISPLAYTITLE: الدالة <code>forEachIndexed()‎</code> في لغة Kotlin}}</noinclude> تُنفّد الدالةُ<code>forEachIndexed()‎</code> ا...')
 
ط
 
(4 مراجعات متوسطة بواسطة 3 مستخدمين غير معروضة)
سطر 1: سطر 1:
<noinclude>{{DISPLAYTITLE: الدالة <code>forEachIndexed()‎</code> في لغة Kotlin}}</noinclude>
+
تُنفّد الدالةُ <code>forEachIndexed()‎</code> الدالةَ المُمرّرةَ إليها على كل عنصر من عناصر [[Kotlin/Array|المصفوفة]]، أو [[Kotlin/collections|المجموعة]]، أو [[Kotlin/collections/Map|الخريطة]]، التي استُدعيت معها وعلى فهرس ذلك العنصر. 
تُنفّد الدالةُ<code>forEachIndexed()‎</code> الدالةَ المُمرّرةَ <code>action</code> على كل عناصر [[Kotlin/Array|المصفوفة]] أو [[Kotlin/collections|المجموعة]] أو [[Kotlin/collections/Map|القاموس]] الذي استُدعيت عبره.  
 
 
==البنية العامة==
 
==البنية العامة==
في حال استدعاء الدالة<code>forEachIndexed()‎</code> على [[Kotlin/Array|المصفوفات]] أو [[Kotlin/collections|المجموعات]] فإنّها تنفّذ الدالة المُمرّرة <code>action</code> على كل عناصرها.<syntaxhighlight lang="kotlin">
+
<syntaxhighlight lang="kotlin">
inline fun <T> Array<out T>.forEach(action: (T) -> Unit)  
+
inline fun <T> Array<out T>.forEachIndexed(
inline fun ByteArray.forEach(action: (Byte) -> Unit)  
+
    action: (index: Int, T) -> Unit)  
inline fun ShortArray.forEach(action: (Short) -> Unit)  
+
inline fun ByteArray.forEachIndexed(
inline fun IntArray.forEach(action: (Int) -> Unit)  
+
    action: (index: Int, Byte) -> Unit)
inline fun LongArray.forEach(action: (Long) -> Unit)  
+
inline fun ShortArray.forEachIndexed(
inline fun FloatArray.forEach(action: (Float) -> Unit)  
+
    action: (index: Int, Short) -> Unit)  
inline fun DoubleArray.forEach(action: (Double) -> Unit)
+
inline fun IntArray.forEachIndexed(
inline fun BooleanArray.forEach(action: (Boolean) -> Unit)  
+
    action: (index: Int, Int) -> Unit)  
inline fun CharArray.forEach(action: (Char) -> Unit)  
+
inline fun LongArray.forEachIndexed(
inline fun <T> Iterable<T>.forEach(action: (T) -> Unit)  
+
    action: (index: Int, Long) -> Unit)  
 +
inline fun FloatArray.forEachIndexed(
 +
    action: (index: Int, Float) -> Unit)  
 +
inline fun DoubleArray.forEachIndexed(
 +
    action: (index: Int, Double) -> Unit) (
 +
inline fun BooleanArray.forEachIndexed(
 +
    action: (index: Int, Boolean) -> Unit) (source)
 +
inline fun CharArray.forEachIndexed(
 +
    action: (index: Int, Char) -> Unit)  
 +
inline fun <T> Iterable<T>.forEachIndexed(
 +
    action: (index: Int, T) -> Unit)  
 +
 
 +
</syntaxhighlight>وجود الكلمة المفتاحية <code>inline</code> يدل على أنَّ هذه الدالة مباشرة (للمزيد من التفاصيل، راجع صفحة <nowiki/>[[Kotlin/inline functions|الدوال المباشرة (inline functions)]]).
 +
 
 +
== المعاملات ==
 +
 
 +
=== <code>action</code> ===
 +
الدالة المراد تنفيذها مع كل عنصر من عناصر [[Kotlin/Array|المصفوفة]]، أو [[Kotlin/collections|المجموعة]]، أو [[Kotlin/collections/Map|الخريطة]] المعطاة. يمرر إليها فهرس العنصر <code>index</code> وقيمته <code>T</code>.
  
</syntaxhighlight>في حال استدعاء الدالة<code>forEachIndexed()‎</code> على [[Kotlin/collections/Map|القواميس]] فإنّها تنفّذ الدالة المُمرّرة <code>action</code> على كل عناصرها.<syntaxhighlight lang="kotlin">
 
inline fun <K, V> Map<out K, V>.forEach(
 
    action: (Entry<K, V>) -> Unit)
 
</syntaxhighlight>في حال استدعاء الدالة<code>forEachIndexed()‎</code> على الكائنات من الصنف <code>[[Kotlin/iterator|iterator]]</code>  فإنّها تنفّذ الدالة المُمرّرة <code>action</code> على كل عناصرها.<syntaxhighlight lang="kotlin">
 
inline fun <T> Iterator<T>.forEach(operation: (T) -> Unit)
 
</syntaxhighlight>يُلاحَظ وجود الكلمة المفتاحية <code>inline</code> للدلالة على أن هذه الدالة مباشرة، وللمزيد من التفاصيل راجع <nowiki/>[[Kotlin/inline functions|توثيق الدوال المباشرة (inline functions)]].
 
 
==أمثلة==
 
==أمثلة==
===استخدام الدالة <code>()forEachIndexed</code> مع المصفوفات===
+
تعرف الشيفرة الآتية <nowiki/>[[Kotlin/Array|مصفوفة]] باسم <code>array</code> باستخدام الدالة <code>()arrayOf</code>، ثم تستخدم الدالة <code>()forEachIndexed</code> لطباعة فهرس وقيمة كل عنصر من عناصر تلك المصفوفة بجانب بعضهما بعضًا:<syntaxhighlight lang="kotlin">
تعرف الشيفرة الآتية <nowiki/>[[Kotlin/Array|مصفوفة]] باسم <code>array</code> مكونة من ستة أعداد باستخدام الدالة <code>()arrayOf</code>، ثم تستخدم الدالة <code>()forEachIndexed</code> على <code>array</code> بتمرير دالة كمُعامل، ثم تطبع الناتج:<syntaxhighlight lang="kotlin">
 
 
fun main(args: Array<String>) {
 
fun main(args: Array<String>) {
val array = arrayOf(1,2,3,4,5,6)
+
    val array = arrayOf(1, 2, 3, 4, 5, 6)
  
array.forEach { print("$it ") } // 1 2 3 4 5 6  
+
    array.forEachIndexed { index, element -> print("$index:$element, ") } // 0:1, 1:2, 2:3, 3:4, 4:5, 5:6,
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
==انظر أيضًا==
 
==انظر أيضًا==
<code>[[Kotlin/collections/forEachIndexed|forEachIndexed()]]</code>‎: تُنفّد الدالةَ المُمرّرةَ على كل عناصر [[Kotlin/Array|المصفوفة]] أو [[Kotlin/collections|المجموعة]] أو [[Kotlin/collections/Map|القاموس]] الذي استُدعيت عبره.
+
* الدالة <code>[[Kotlin/collections/forEach|forEach()]]</code>‎: تُنفّد الدالةَ المُمرّرةَ إليها على كل عنصر من عناصر [[Kotlin/Array|المصفوفة]] أو [[Kotlin/collections|المجموعة]] أو [[Kotlin/collections/Map|الخريطة]] التي استُدعيت معها.
 +
* التابع <code>[[Kotlin/Array/iterator|Array.iterator()‎]]</code>: يعيد عنصرًا تكراريًّا (iterator) بهدف تكرار بنيةٍ (block) برمجيةٍ على كافّة عناصر المصفوفة.
 +
* الدالة <code>[[Kotlin/collections/elementAt|elementAt()]]‎</code>: تجلب عنصرًا ذي فهرس معيَّن من <nowiki/>[[Kotlin/Array|المصفوفة]] أو <nowiki/>[[Kotlin/collections|المجموعة]] التي استُدعيت معها. 
 +
* الخاصية <code>‎‎[[Kotlin/collections/indices|indices]]</code>: تُستخدَم هذه الخاصيّة للحصول على الفهارس الصحيحة (valid indices) للمصفوفة أو المجموعة (collection).
  
 
==مصادر==
 
==مصادر==
*[https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/for-each.html الدالة  forEachIndexed()‎ في التوثيق الرسميّ للمكتبة القياسيّة في لغة Kotlin.]
+
*[https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/for-each-indexed.html صفحة الدالة forEachIndexed()‎ في التوثيق الرسميّ للمكتبة القياسيّة في لغة Kotlin.]<noinclude>{{DISPLAYTITLE: الدالة <code>forEachIndexed()‎</code> في Kotlin}}</noinclude>
 
[[تصنيف:Kotlin]]
 
[[تصنيف:Kotlin]]
[[تصنيف:Kotlin Functions]]
+
[[تصنيف:Kotlin Function]]
 +
[[تصنيف:Kotlin Collection]]

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

تُنفّد الدالةُ forEachIndexed()‎ الدالةَ المُمرّرةَ إليها على كل عنصر من عناصر المصفوفة، أو المجموعة، أو الخريطة، التي استُدعيت معها وعلى فهرس ذلك العنصر. 

البنية العامة

inline fun <T> Array<out T>.forEachIndexed(
    action: (index: Int, T) -> Unit) 
inline fun ByteArray.forEachIndexed(
    action: (index: Int, Byte) -> Unit)
inline fun ShortArray.forEachIndexed(
    action: (index: Int, Short) -> Unit) 
inline fun IntArray.forEachIndexed(
    action: (index: Int, Int) -> Unit) 
inline fun LongArray.forEachIndexed(
    action: (index: Int, Long) -> Unit) 
inline fun FloatArray.forEachIndexed(
    action: (index: Int, Float) -> Unit) 
inline fun DoubleArray.forEachIndexed(
    action: (index: Int, Double) -> Unit) (
inline fun BooleanArray.forEachIndexed(
    action: (index: Int, Boolean) -> Unit) (source)
inline fun CharArray.forEachIndexed(
    action: (index: Int, Char) -> Unit) 
inline fun <T> Iterable<T>.forEachIndexed(
    action: (index: Int, T) -> Unit)

وجود الكلمة المفتاحية inline يدل على أنَّ هذه الدالة مباشرة (للمزيد من التفاصيل، راجع صفحة الدوال المباشرة (inline functions)).

المعاملات

action

الدالة المراد تنفيذها مع كل عنصر من عناصر المصفوفة، أو المجموعة، أو الخريطة المعطاة. يمرر إليها فهرس العنصر index وقيمته T.

أمثلة

تعرف الشيفرة الآتية مصفوفة باسم array باستخدام الدالة ()arrayOf، ثم تستخدم الدالة ()forEachIndexed لطباعة فهرس وقيمة كل عنصر من عناصر تلك المصفوفة بجانب بعضهما بعضًا:

fun main(args: Array<String>) {
    val array = arrayOf(1, 2, 3, 4, 5, 6)

    array.forEachIndexed { index, element -> print("$index:$element, ") } // 0:1, 1:2, 2:3, 3:4, 4:5, 5:6,
}

انظر أيضًا

  • الدالة forEach()‎: تُنفّد الدالةَ المُمرّرةَ إليها على كل عنصر من عناصر المصفوفة أو المجموعة أو الخريطة التي استُدعيت معها.
  • التابع Array.iterator()‎: يعيد عنصرًا تكراريًّا (iterator) بهدف تكرار بنيةٍ (block) برمجيةٍ على كافّة عناصر المصفوفة.
  • الدالة elementAt(): تجلب عنصرًا ذي فهرس معيَّن من المصفوفة أو المجموعة التي استُدعيت معها. 
  • الخاصية ‎‎indices: تُستخدَم هذه الخاصيّة للحصول على الفهارس الصحيحة (valid indices) للمصفوفة أو المجموعة (collection).

مصادر