الدالة subtract()‎ في Kotlin

من موسوعة حسوب
< Kotlin‏ | collections
مراجعة 11:36، 30 أغسطس 2018 بواسطة عبد اللطيف ايمش (نقاش | مساهمات) (استبدال النص - 'Kotlin Functions' ب'Kotlin Function')
اذهب إلى التنقل اذهب إلى البحث

تستثني الدالة subtract()‎ العناصر المشتركة بين كائنين.

تحافظ المجموعة (Set) المُعادة على ترتيب التكرار الخاص بالمجموعة (collection) المعطاة.

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

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

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

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

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

تُعاد مجموعة (Set) تضم جميع العناصر الموجودة في المصفوفة أو المجموعة التكرارية المعطاة والغير موجودة في الوسيط other المعطى.

أمثلة

استعمال الدالة ()subtract لاستثناء العناصر المشتركة بين كائنين:

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

    println(array.subtract(list))// [5, 7, 2, 6]
}

انظر أيضًا

مصادر