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

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
(أنشأ الصفحة ب'<noinclude>{{DISPLAYTITLE: الدالة <code>union()‎</code> في لغة Kotlin}}</noinclude> تعيد الدالة<code>union()‎</code> لائحة...')
 
ط
 
(5 مراجعات متوسطة بواسطة 3 مستخدمين غير معروضة)
سطر 1: سطر 1:
<noinclude>{{DISPLAYTITLE: الدالة <code>union()‎</code> في لغة Kotlin}}</noinclude>
+
<noinclude>{{DISPLAYTITLE: الدالة <code>union()‎</code> في Kotlin}}</noinclude>
تعيد الدالة<code>union()‎</code> [[Kotlin/List|لائحة]] تضم جميع عناصر [[Kotlin/Array|المصفوفة]]  أو  [[Kotlin/Iterable|المجموعة التكرارية]]  التي استُدعيت عبرها. وفي حال استدعاء الدالة<code>toList()‎</code> عبر [[Kotlin/Map|قاموس]] فستعيد [[Kotlin/List|لائحة]] تضم جميع الأزواج مفتاح/قيمة المنتمية إليه.
+
<nowiki/>تجمع الدالة <code>union()‎</code> العناصر المشتركة وغير المتشركة بين [[Kotlin/Array|المصفوفة]] أو [[Kotlin/Iterable|المجموعة التكرارية]] التي استُدعيت معها وبين [[Kotlin/Iterable|المجموعة التكرارية]] المُمرّرة إليها في مجموعةٍ واحدةٍ من النوع <code>[[Kotlin/collections/Set|Set]]</code> بعد حذف العناصر المتكررة.
 +
 
 +
تحافظ المجموعة المُعادة على نفس ترتيب تكرار [[Kotlin/Array|المصفوفة]] أو [[Kotlin/Iterable|المجموعة التكرارية]] الأصلية. تضاف عناصر [[Kotlin/Iterable|المجموعة التكرارية]] الممرَّرة إلى الدالة وغير المتكررة في النهاية.
 
==البنية العامة==
 
==البنية العامة==
يمكن استدعاء الدالة<code>union()‎</code> عبر [[Kotlin/Array|المصفوفات]]  أو  [[Kotlin/Iterable|المجموعات التكرارية]]:<syntaxhighlight lang="kotlin">
+
يمكن استدعاء الدالة<code>union()‎</code> مع [[Kotlin/Array|المصفوفات]] و<nowiki/>[[Kotlin/Iterable|المجموعات التكرارية]]:<syntaxhighlight lang="kotlin">
fun <T> Array<out T>.toList(): List<T>  
+
infix fun <T> Array<out T>.union(other: Iterable<T>): Set<T>  
fun ByteArray.toList(): List<Byte>  
+
infix fun ByteArray.union(other: Iterable<Byte>): Set<Byte>  
fun ShortArray.toList(): List<Short>  
+
infix fun ShortArray.union(
fun IntArray.toList(): List<Int>  
+
    other: Iterable<Short>
fun LongArray.toList(): List<Long>
+
): Set<Short>  
fun FloatArray.toList(): List<Float>  
+
infix fun IntArray.union(other: Iterable<Int>): Set<Int>
fun DoubleArray.toList(): List<Double>  
+
infix fun LongArray.union(other: Iterable<Long>): Set<Long>
fun BooleanArray.toList(): List<Boolean>  
+
infix fun FloatArray.union(
fun CharArray.toList(): List<Char>
+
    other: Iterable<Float>
fun <T> Iterable<T>.toList(): List<T>  
+
): Set<Float>  
</syntaxhighlight>يمكن استدعاء الدالة<code>union()‎</code>  عبر [[Kotlin/Map|القواميس]]:<syntaxhighlight lang="kotlin">
+
infix fun DoubleArray.union(
fun <K, V> Map<out K, V>.toList(): List<Pair<K, V>>
+
    other: Iterable<Double>
 +
): Set<Double>  
 +
infix fun BooleanArray.union(
 +
    other: Iterable<Boolean>
 +
): Set<Boolean>  
 +
infix fun CharArray.union(other: Iterable<Char>): Set<Char>  
 +
 
 +
infix fun <T> Iterable<T>.union(other: Iterable<T>): Set<T>
  
 
</syntaxhighlight>
 
</syntaxhighlight>
==القيمة المُعادة==
+
 
[[Kotlin/List|لائحة]] تضم جميع عناصر [[Kotlin/Array|المصفوفة]]  أو  [[Kotlin/Iterable|المجموعة التكرارية]]  التي استُدعيت عبرها. وفي حال استدعاء الدالة<code>toList()‎</code> عبر [[Kotlin/Map|قاموس]] فستعيد [[Kotlin/List|لائحة]] تضم جميع الأزواج مفتاح/قيمة المنتمية إليه.
+
== المعاملات ==
 +
 
 +
=== <code>other</code> ===
 +
[[Kotlin/Iterable|المجموعة التكرارية]] الأخرى التي ستطبق عليها العملية.
 +
 
 +
==القيمة المعادة==
 +
تعاد مجموعة من النوع [[Kotlin/collections/Set|<code>Set</code>]] تحوي جميع عناصر [[Kotlin/Array|المصفوفة]] أو [[Kotlin/Iterable|المجموعة التكرارية]] المعطاة و<nowiki/>[[Kotlin/Iterable|المجموعة التكرارية]] المُمرّرة.
 
==أمثلة==
 
==أمثلة==
===استخدام الدالة <code>()union</code> مع المصفوفات===
+
استعمال الدالة <code>()union</code> لجمع العناصر المشتركة وغير المتشركة بين مصفوفة وقائمة مع حذف العناصر المتكررة:<syntaxhighlight lang="kotlin">
تعرّف الشيفرة الآتية  <nowiki/>[[Kotlin/Array|مصفوفة]] باسم <code>array</code>  مكونة من ثلاثة أعداد باستخدام الدالة <code>()arrayOf‎</code>، ثم تستخدم الدالة<code>()union</code> على <code>array</code>، ثم تطبع الناتج:<syntaxhighlight lang="kotlin">
 
 
fun main(args: Array<String>) {
 
fun main(args: Array<String>) {
val array = arrayOf(4, 5, 6)
+
    val array = arrayOf(4, 5, 6, 4, 1, 6)
 +
    val mutableList = mutableListOf(1, 2, 3)
  
println(array.toList()) // [4, 5, 6]
+
    println(array.union(mutableList)) // [4, 5, 6, 1, 2, 3]
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
==أنظر أيضًا==
+
==انظر أيضًا==
*<code>[[Kotlin/collections/toBooleanArray|toBooleanArray()]]</code>‎ : تعيد [[Kotlin/Array|مصفوفة]] من القيم المنطقية (من النوع <code>BooleanArray</code>) تضم جميع عناصر <nowiki/>[[Kotlin/Array|المصفوفة العامة]] (أي من النوع<code>Array<out Boolean>‎</code>) أو <nowiki/>[[Kotlin/collections|المجموعة]]  التي استُدعيت عبرها.
+
*الدالة <code>[[Kotlin/collections/subtract|subtract()]]</code>: تستثني العناصر المشتركة بين كائنين.
*<code>[[Kotlin/collections/toByteArray|toByteArray()]]</code>‎ : تعيد <nowiki/>[[Kotlin/Array|مصفوفة]] من النوع <code>ByteArray</code>تضم جميع عناصر <nowiki/>[[Kotlin/Array|المصفوفة العامة]] (أي من النوع <code>Array<Byte>‎</code>) أو <nowiki/>[[Kotlin/collections|المجموعة]]  التي استُدعيت عبرها.
+
* الدالة <code>[[Kotlin/collections/sum|sum()]]</code>: تجمع جميع قيم عناصر <nowiki/>[[Kotlin/Array|المصفوفة]] أو <nowiki/>[[Kotlin/collections|المجموعة]] التي استُدعيت معها ثمَّ تعيد الناتج.
*<code>[[Kotlin/collections/toDoubleArray|toDoubleArray()]]</code>‎ : تعيد [[Kotlin/Array|مصفوفة]] من  النوع <code>DoubleArray</code>تضم جميع عناصر [[Kotlin/Array|المصفوفة العامة]] (أي من النوع <code>Array<Double>‎</code>) أو  [[Kotlin/collections|المجموعة]]  التي استُدعيت عبرها.
+
* الدالة <code>[[Kotlin/collections/plusElement|plusElement()]]</code>: تضيف العنصر المُمرّر إليها إلى <nowiki/>[[Kotlin/Array|المصفوفة]] أو <nowiki/>[[Kotlin/collections|المجموعة]] التي استُدعيت معها.
 
==مصادر==
 
==مصادر==
* [https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/to-list.html الدالة  union()‎ في التوثيق الرسميّ للمكتبة القياسيّة في لغة Kotlin.]
+
*[https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/union.html صفحة الدالة union()‎ في التوثيق الرسميّ للمكتبة القياسيّة في لغة Kotlin.]
 
[[تصنيف:Kotlin]]
 
[[تصنيف:Kotlin]]
[[تصنيف:Kotlin Functions]]
+
[[تصنيف:Kotlin Function]]
 +
[[تصنيف:Kotlin Collection]]

المراجعة الحالية بتاريخ 11:53، 8 سبتمبر 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> 

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

المعاملات

other

المجموعة التكرارية الأخرى التي ستطبق عليها العملية.

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

تعاد مجموعة من النوع Set تحوي جميع عناصر المصفوفة أو المجموعة التكرارية المعطاة والمجموعة التكرارية المُمرّرة.

أمثلة

استعمال الدالة ()union لجمع العناصر المشتركة وغير المتشركة بين مصفوفة وقائمة مع حذف العناصر المتكررة:

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]
}

انظر أيضًا

مصادر