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

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
سطر 1: سطر 1:
 
<noinclude>{{DISPLAYTITLE: الدالة <code>asList()‎</code> في لغة Kotlin}}</noinclude>
 
<noinclude>{{DISPLAYTITLE: الدالة <code>asList()‎</code> في لغة Kotlin}}</noinclude>
تُنشئ الدالة <code>asList()‎</code>نسخة تكرارية (Iterable instance) والتي تُغلّف المصفوفة الاصلية وتعيد عناصرها عندما تُستخدم في حلقات التكرار (مثل <code>[[Kotlin/control flow#.D8.AA.D8.B9.D8.A8.D9.8A.D8.B1 for|for]]</code>).
+
تُعيد الدالة <code>asList()‎</code> لائحة (list) والتي تُغلّف المصفوفة الاصلية التي استُدعيت عبرها.
 
==البنية العامة==
 
==البنية العامة==
في حال استدعاء الدالة <code>asList()‎</code>عبر مصفوفة فستعيد نسخة تكرارية (Iterable instance) والتي تُغلّف المصفوفة التي استُدعيت عبرها وتعيد عناصرها عندما تُستخدم في حلقات التكرار (مثل <code>[[Kotlin/control flow#.D8.AA.D8.B9.D8.A8.D9.8A.D8.B1 for|for]]</code>):<syntaxhighlight lang="kotlin">
+
<syntaxhighlight lang="kotlin">
fun <T> Array<out T>.asIterable(): Iterable<T>
+
fun <T> Array<out T>.asList(): List<T>
fun ByteArray.asIterable(): Iterable<Byte>  
+
fun ByteArray.asList(): List<Byte>  
fun ShortArray.asIterable(): Iterable<Short>  
+
fun ShortArray.asList(): List<Short>  
fun IntArray.asIterable(): Iterable<Int>  
+
fun IntArray.asList(): List<Int>  
fun LongArray.asIterable(): Iterable<Long>
+
fun LongArray.asList(): List<Long>  
fun FloatArray.asIterable(): Iterable<Float>  
+
fun FloatArray.asList(): List<Float>  
fun DoubleArray.asIterable(): Iterable<Double>  
+
fun DoubleArray.asList(): List<Double>  
fun BooleanArray.asIterable(): Iterable<Boolean>  
+
fun BooleanArray.asList(): List<Boolean>  
fun CharArray.asIterable(): Iterable<Char>  
+
fun CharArray.asList(): List<Char>
</syntaxhighlight>يمكن استدعاء الدالة <code>asIterable()‎</code>عبر مجموعة (collection) وستُعيدها على شكل مُكرّر (iterable):<syntaxhighlight lang="kotlin">
+
</syntaxhighlight>
inline fun <T> Iterable<T>.asIterable(): Iterable<T>
 
 
 
</syntaxhighlight>يمكن استدعاء الدالة <code>asIterable()‎</code>عبر قاموس (Map) وستعيد نسخة تكرارية (Iterable instance) والتي تُغلّف القاموس الاصلي وتعيد مداخله (entries) عندما تُستخدم في حلقات التكرار (مثل <code>[[Kotlin/control flow#.D8.AA.D8.B9.D8.A8.D9.8A.D8.B1 for|for]]</code>)::<syntaxhighlight lang="kotlin">
 
inline fun <K, V> Map<out K, V>.asIterable(): Iterable<Entry<K, V>>
 
 
 
</syntaxhighlight>يُلاحَظ وجود الكلمة المفتاحية <code>inline</code> للدلالة على أن هذه الدالة مباشرة، وللمزيد من التفاصيل راجع [[Kotlin/inline functions|توثيق الدوال المباشرة (inline functions)]].
 
 
==القيم المعادة==
 
==القيم المعادة==
نسخة تكرارية (Iterable instance). (انظر شرح الدالة أعلاه لمزيد من التفاصيل)
+
لائحة (list) تُغلّف المصفوفة الاصلية التي استُدعيت عبرها.
 
==أمثلة==
 
==أمثلة==
===استخدام الدالة <code>()asIterable</code> مع المصفوفات===
+
===استخدام الدالة <code>()</code><code>asList</code>مع المصفوفات===
تعرف الشيفرة الآتية مصفوفة باسم <code>array</code> مكونة من أربعة أعداد باستخدام الدالة <code>()arrayOf</code>، ثم تنشئ مُكرّرا باسم <code>iter</code> باستخدام الدالة <code>()asIterable</code>ثم تستخدم ذلك المكرر في حلقة تكرارية <code>[[Kotlin/control flow#.D8.AA.D8.B9.D8.A8.D9.8A.D8.B1 for|for]]</code>:<syntaxhighlight lang="kotlin">
+
تعرف الشيفرة الآتية مصفوفة باسم <code>array</code> مكونة من حرفين باستخدام الدالة <code>()arrayOf</code>، ثم تنشئ لائحة من المصفوفة <code>array</code> باسم <code>list</code> باستخدام <code>()</code><code>asList</code> ثم تطبع تلك اللائحة:<syntaxhighlight lang="kotlin">
 
fun main(args: Array<String>) {
 
fun main(args: Array<String>) {
val array = arrayOf(6,7,8,9) // انشاء مصفوفة
+
    val array = arrayOf("a", "b")
val iter = array.asIterable() // انشاء مكرر
+
    val list = array.asList()
+
 
for (i in iter) print(i) // 6789
+
    println(list) // [a, b]
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
==انظر أيضًا==
 
==انظر أيضًا==
*<code>[[Kotlin/collections/asList|asList()]]</code>‎: تعيد لائحة والتي تُغلّف المصفوفة الاصلية.
+
*<code>[[Kotlin/collections/asIterable|asIterable()]]</code>‎: تُنشئ نسخة تكرارية (Iterable instance) والتي تُغلّف المصفوفة الاصلية وتعيد عناصرها عندما تُستخدم في حلقات التكرار (مثل <code>[[Kotlin/control flow#.D8.AA.D8.B9.D8.A8.D9.8A.D8.B1 for|for]]</code>).
 
*<code>[[Kotlin/collections/asSequence|asSequence()]]</code>‎: تعيد نسخة من النوع Sequence  والتي تُغلّف المصفوفة الاصلية وتعيد عناصرها عندما تطبق تستخدم في حلقات التكرار (مثل <code>[[Kotlin/control flow#.D8.AA.D8.B9.D8.A8.D9.8A.D8.B1 for|for]]</code>).
 
*<code>[[Kotlin/collections/asSequence|asSequence()]]</code>‎: تعيد نسخة من النوع Sequence  والتي تُغلّف المصفوفة الاصلية وتعيد عناصرها عندما تطبق تستخدم في حلقات التكرار (مثل <code>[[Kotlin/control flow#.D8.AA.D8.B9.D8.A8.D9.8A.D8.B1 for|for]]</code>).
 
==مصادر==
 
==مصادر==
 
<span> </span>
 
<span> </span>
*[https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/as-iterable.html الدالة  asIterable()في التوثيق الرسميّ للمكتبة القياسيّة في لغة Kotlin.]
+
*[https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/as-list.html الدالة  ()asList‎ في التوثيق الرسميّ للمكتبة القياسيّة في لغة Kotlin.]
 
[[تصنيف:Kotlin]]
 
[[تصنيف:Kotlin]]
 
[[تصنيف:Kotlin Functions]]
 
[[تصنيف:Kotlin Functions]]

مراجعة 14:26، 10 مايو 2018

تُعيد الدالة asList()‎ لائحة (list) والتي تُغلّف المصفوفة الاصلية التي استُدعيت عبرها.

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

fun <T> Array<out T>.asList(): List<T>
fun ByteArray.asList(): List<Byte> 
fun ShortArray.asList(): List<Short> 
fun IntArray.asList(): List<Int> 
fun LongArray.asList(): List<Long> 
fun FloatArray.asList(): List<Float> 
fun DoubleArray.asList(): List<Double> 
fun BooleanArray.asList(): List<Boolean> 
fun CharArray.asList(): List<Char>

القيم المعادة

لائحة (list) تُغلّف المصفوفة الاصلية التي استُدعيت عبرها.

أمثلة

استخدام الدالة ()asListمع المصفوفات

تعرف الشيفرة الآتية مصفوفة باسم array مكونة من حرفين باستخدام الدالة ()arrayOf، ثم تنشئ لائحة من المصفوفة array باسم list باستخدام ()asList ثم تطبع تلك اللائحة:

fun main(args: Array<String>) {
    val array = arrayOf("a", "b")
    val list = array.asList()

    println(list) // [a, b]
}

انظر أيضًا

  • asIterable()‎: تُنشئ نسخة تكرارية (Iterable instance) والتي تُغلّف المصفوفة الاصلية وتعيد عناصرها عندما تُستخدم في حلقات التكرار (مثل for).
  • asSequence()‎: تعيد نسخة من النوع Sequence والتي تُغلّف المصفوفة الاصلية وتعيد عناصرها عندما تطبق تستخدم في حلقات التكرار (مثل for).

مصادر