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

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
سطر 31: سطر 31:
 
==أمثلة==
 
==أمثلة==
 
===استخدام الدالة <code>()union</code> مع المصفوفات===
 
===استخدام الدالة <code>()union</code> مع المصفوفات===
تعرّف الشيفرة الآتية  <nowiki/>[[Kotlin/Array|مصفوفة]] باسم <code>array</code>  مكونة من ستة أعداد باستخدام الدالة <code>()arrayOf‎</code>، ثم تعرّف   <nowiki/>[[Kotlin/mutableList|لائحة متغيرة]] باسم <code>mutablelistOf</code>  مكونة من ثلاثة أعداد باستخدام الدالة <code>()mutableListOf</code>، ثم تستخدم الدالة<code>()union</code> على <code>array</code> وتمرر <code>mutablelistOf </code> كوسيط، ثم تطبع الناتج:<syntaxhighlight lang="kotlin">
+
تعرّف الشيفرة الآتية  <nowiki/>[[Kotlin/Array|مصفوفة]] باسم <code>array</code>  مكونة من ستة أعداد باستخدام الدالة <code>()arrayOf‎</code>، ثم تعرّف   <nowiki/>[[Kotlin/mutableList|لائحة متغيرة]] باسم <code>mutablelist</code>  مكونة من ثلاثة أعداد باستخدام الدالة <code>()mutableListOf</code>، ثم تستخدم الدالة<code>()union</code> على <code>array</code> وتمرر <code>mutablelistOf </code> كوسيط، ثم تطبع الناتج:<syntaxhighlight lang="kotlin">
 
fun main(args: Array<String>) {
 
fun main(args: Array<String>) {
 
val array = arrayOf(4, 5, 6, 4, 1, 6)
 
val array = arrayOf(4, 5, 6, 4, 1, 6)

مراجعة 11:49، 24 يونيو 2018

تعيد الدالةunion()‎ مجموعة من النوع Set تضم جميع عناصر المصفوفة  أو المجموعة التكرارية  التي استُدعيت عبرها والمجموعة التكرارية المُمرّرة.

تحافظ المجموعة المُعادة على ترتيب التكرار الأصلي للمصفوفة  أو المجموعة التكرارية التي استُدعيت عبرها، عناصر المجموعة التكرارية المُمرّرة الفريدة ستوضع في النهاية.

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

يمكن استدعاء الدالةunion()‎ عبر المصفوفات:

infix fun <T> Array<out T>.union(other: Iterable<T>): Set<T> 
infix fun ByteArray.union(other: Iterable<Byte>): Set<Byte> 
infix fun ShortArray.union(
    other: Iterable<Short>
): Set<Short> 
infix fun IntArray.union(other: Iterable<Int>): Set<Int>
infix fun LongArray.union(other: Iterable<Long>): Set<Long>
infix fun FloatArray.union(
    other: Iterable<Float>
): Set<Float> 
infix fun DoubleArray.union(
    other: Iterable<Double>
): Set<Double> 
infix fun BooleanArray.union(
    other: Iterable<Boolean>
): Set<Boolean> 
infix fun CharArray.union(other: Iterable<Char>): Set<Char>

يمكن استدعاء الدالةunion()‎ عبر المجموعات التكرارية:

infix fun <T> Iterable<T>.union(other: Iterable<T>): Set<T>

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

مجموعة من النوع Set تضم جميع عناصر المصفوفة أو المجموعة التكرارية  التي استُدعيت عبرها والمجموعة التكرارية المُمرّرة.

أمثلة

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

تعرّف الشيفرة الآتية  مصفوفة باسم array  مكونة من ستة أعداد باستخدام الدالة ()arrayOf‎، ثم تعرّف   لائحة متغيرة باسم mutablelist  مكونة من ثلاثة أعداد باستخدام الدالة ()mutableListOf، ثم تستخدم الدالة()union على array وتمرر mutablelistOf  كوسيط، ثم تطبع الناتج:

fun main(args: Array<String>) {
val array = arrayOf(4, 5, 6, 4, 1, 6)
val mutableList = mutableListOf(1, 2, 3)
  
println(array.union(mutableList)) // [4, 5, 6, 1, 2, 3]
}

أنظر أيضًا

مصادر