Kotlin/collections/toCollection
< Kotlin | collections
تضيف الدالةtoCollection() عناصر المصفوفة أو المجموعة التي استُدعيت عبرها إلى المجموعة المتغيرة (MutableCollection) المُمرّرة destination.
البنية العامة
fun <T, C : MutableCollection<in T>> Array<out T>.toCollection(
destination: C
): C
fun <C : MutableCollection<in Byte>> ByteArray.toCollection(
destination: C
): C
fun <C : MutableCollection<in Short>> ShortArray.toCollection(
destination: C
): C
fun <C : MutableCollection<in Int>> IntArray.toCollection(
destination: C
): C
fun <C : MutableCollection<in Long>> LongArray.toCollection(
destination: C
): C
fun <C : MutableCollection<in Float>> FloatArray.toCollection(
destination: C
): C
fun <C : MutableCollection<in Double>> DoubleArray.toCollection(
destination: C
): C
fun <C : MutableCollection<in Boolean>> BooleanArray.toCollection(
destination: C
): C
fun <C : MutableCollection<in Char>> CharArray.toCollection(
destination: C
): C
fun <T, C : MutableCollection<in T>> Iterable<T>.toCollection(
destination: C
): C
القيمة المُعادة
المجموعة المتغيرة (MutableCollection) المُمرّرة destination بعد إضافة عناصر المصفوفة أو المجموعة التي استُدعيت عبرها الدالة.
أمثلة
استخدام الدالة ()toCollection مع المصفوفات
تعرّف الشيفرة الآتية مصفوفة باسم array مكونة من ثلاثة أعداد باستخدام الدالة ()arrayOf، ثم تعرّف لائحة متغيرة باسم list مكونة من ثلاثة أعداد باستخدام الدالة ()mutableListOf، ثم تستخدم الدالة ()toCollection على array وتمرر list كوسيط، ثم تطبع الناتج:
fun main(args: Array<String>) {
val array = arrayOf(4, 5, 6)
var list = mutableListOf(8, 7, 3)
println(array.toCollection(list)) // [8, 7, 3, 4, 5, 6]
}
أنظر أيضًا
toBooleanArray() : تعيد مصفوفة من القيم المنطقية (من النوعBooleanArray) تضم جميع عناصر المصفوفة العامة (أي من النوعArray<out Boolean>) أو المجموعة التي استُدعيت عبرها.toByteArray() : تعيد مصفوفة من النوعByteArrayتضم جميع عناصر المصفوفة العامة (أي من النوعArray<Byte>) أو المجموعة التي استُدعيت عبرها.toDoubleArray() : تعيد مصفوفة من النوعDoubleArrayتضم جميع عناصر المصفوفة العامة (أي من النوعArray<Double>) أو المجموعة التي استُدعيت عبرها.