الفرق بين المراجعتين ل"Kotlin/collections/slice"
< Kotlin | collections
اذهب إلى التنقل
اذهب إلى البحث
سطر 1: | سطر 1: | ||
<noinclude>{{DISPLAYTITLE: الدالة <code>slice()</code> في لغة Kotlin}}</noinclude> | <noinclude>{{DISPLAYTITLE: الدالة <code>slice()</code> في لغة Kotlin}}</noinclude> | ||
− | تعيد الدالة<code>slice()</code>[[Kotlin/List|لائحة]] | + | تعيد الدالة<code>slice()</code>[[Kotlin/List|لائحة]] تحتوي عناصر [[Kotlin/Array|المصفوفة]] أو [[Kotlin/List|اللائحة]] التي استُدعيت عبرها والموجودة عند الفهارس التي يحددها الوسيط الممرّر <code>indices</code>. |
==البنية العامة== | ==البنية العامة== | ||
− | + | يمكن تمرير [[Kotlin/ranges|مجال]] كوسيط إلى الدالة <code>slice()</code>:<nowiki/><nowiki/><syntaxhighlight lang="kotlin"> | |
− | fun <T> Array<T>. | + | fun <T> Array<out T>.slice(indices: IntRange): List<T> |
− | + | fun ByteArray.slice(indices: IntRange): List<Byte> | |
− | ): | + | fun ShortArray.slice(indices: IntRange): List<Short> |
− | fun ByteArray. | + | fun IntArray.slice(indices: IntRange): List<Int> |
− | fun ShortArray. | + | fun LongArray.slice(indices: IntRange): List<Long> |
− | + | fun FloatArray.slice(indices: IntRange): List<Float> | |
− | + | fun DoubleArray.slice(indices: IntRange): List<Double> | |
− | fun IntArray. | + | fun BooleanArray.slice(indices: IntRange): List<Boolean> |
− | fun LongArray. | + | fun CharArray.slice(indices: IntRange): List<Char> |
− | fun FloatArray. | + | fun <T> List<T>.slice(indices: IntRange): List<T> |
− | + | </syntaxhighlight>يمكن تمرير كائن من النوع <code>Iterable</code> كوسيط إلى الدالة <code>slice()</code>:<nowiki/><nowiki/><syntaxhighlight lang="kotlin"> | |
− | + | fun <T> Array<out T>.slice(indices: Iterable<Int>): List<T> | |
− | fun DoubleArray. | + | fun ByteArray.slice(indices: Iterable<Int>): List<Byte> |
− | + | fun ShortArray.slice(indices: Iterable<Int>): List<Short> | |
− | + | fun IntArray.slice(indices: Iterable<Int>): List<Int> | |
− | fun BooleanArray. | + | fun LongArray.slice(indices: Iterable<Int>): List<Long> |
− | + | fun FloatArray.slice(indices: Iterable<Int>): List<Float> | |
− | ): | + | fun DoubleArray.slice(indices: Iterable<Int>): List<Double> |
− | fun | + | fun BooleanArray.slice(indices: Iterable<Int>): List<Boolean> |
− | + | fun CharArray.slice(indices: Iterable<Int>): List<Char> | |
− | </syntaxhighlight> | ||
− | fun <T> Array<out T>.slice(indices: Iterable<Int>): List<T> | ||
− | fun ByteArray.slice(indices: Iterable<Int>): List<Byte> | ||
− | fun ShortArray.slice(indices: Iterable<Int>): List<Short> | ||
− | fun IntArray.slice(indices: Iterable<Int>): List<Int> | ||
− | fun LongArray.slice(indices: Iterable<Int>): List<Long> | ||
− | fun FloatArray.slice(indices: Iterable<Int>): List<Float> | ||
− | fun DoubleArray.slice(indices: Iterable<Int>): List<Double> | ||
− | fun BooleanArray.slice(indices: Iterable<Int>): List<Boolean> | ||
− | fun CharArray.slice(indices: Iterable<Int>): List<Char> | ||
fun <T> List<T>.slice(indices: Iterable<Int>): List<T> | fun <T> List<T>.slice(indices: Iterable<Int>): List<T> | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==القيم المٌعادة== | ==القيم المٌعادة== | ||
− | + | [[Kotlin/List|لائحة]] تحتوي عناصر [[Kotlin/Array|المصفوفة]] أو [[Kotlin/List|اللائحة]]<nowiki/>التي استُدعيت عبرها والموجودة عند الفهارس التي يحددها الوسيط الممرّر <code>indices</code>. | |
==أمثلة== | ==أمثلة== | ||
===استخدام الدالة <code>()slice</code> مع المصفوفات=== | ===استخدام الدالة <code>()slice</code> مع المصفوفات=== | ||
تعرف الشيفرة الآتية <nowiki/>[[Kotlin/Array|مصفوفة]]<nowiki/>باسم <code>array</code> مكونة من ستة عناصر باستخدام الدالة <code>()arrayOf</code>، ثم تستخدم الدالة<code>()slice</code> على <code>array</code> مع تمرير [[Kotlin/ranges|مجال]]، ثم تطبع الناتج:<syntaxhighlight lang="kotlin"> | تعرف الشيفرة الآتية <nowiki/>[[Kotlin/Array|مصفوفة]]<nowiki/>باسم <code>array</code> مكونة من ستة عناصر باستخدام الدالة <code>()arrayOf</code>، ثم تستخدم الدالة<code>()slice</code> على <code>array</code> مع تمرير [[Kotlin/ranges|مجال]]، ثم تطبع الناتج:<syntaxhighlight lang="kotlin"> | ||
− | |||
− | |||
fun main(args: Array<String>) { | fun main(args: Array<String>) { | ||
val array = arrayOf(1, 2, 3, 4, 5, 6) | val array = arrayOf(1, 2, 3, 4, 5, 6) | ||
− | print | + | print (array.slice(1..3)) // [2, 3, 4] |
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
===أنظر أيضًا=== | ===أنظر أيضًا=== | ||
− | *<code>[[Kotlin/collections/ | + | *<code>[[Kotlin/collections/sliceArray|sliceArray()]]</code> : تعيد [[Kotlin/List|لائحة]] أو [[Kotlin/Array|مصفوفة]] تحتوي عناصر [[Kotlin/Array|المصفوفة]] التي استُدعيت عبرها والموجودة عند الفهارس التي يحددها الوسيط الممرّر <code>indices</code>. |
==مصادر== | ==مصادر== | ||
*[https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/slice.html الدالة slice() في التوثيق الرسميّ للمكتبة القياسيّة في لغة Kotlin.] | *[https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/slice.html الدالة slice() في التوثيق الرسميّ للمكتبة القياسيّة في لغة Kotlin.] | ||
[[تصنيف:Kotlin]] | [[تصنيف:Kotlin]] | ||
[[تصنيف:Kotlin Functions]] | [[تصنيف:Kotlin Functions]] |
مراجعة 12:44، 4 يونيو 2018
تعيد الدالةslice()
لائحة تحتوي عناصر المصفوفة أو اللائحة التي استُدعيت عبرها والموجودة عند الفهارس التي يحددها الوسيط الممرّر indices
.
البنية العامة
يمكن تمرير مجال كوسيط إلى الدالة slice()
:
fun <T> Array<out T>.slice(indices: IntRange): List<T>
fun ByteArray.slice(indices: IntRange): List<Byte>
fun ShortArray.slice(indices: IntRange): List<Short>
fun IntArray.slice(indices: IntRange): List<Int>
fun LongArray.slice(indices: IntRange): List<Long>
fun FloatArray.slice(indices: IntRange): List<Float>
fun DoubleArray.slice(indices: IntRange): List<Double>
fun BooleanArray.slice(indices: IntRange): List<Boolean>
fun CharArray.slice(indices: IntRange): List<Char>
fun <T> List<T>.slice(indices: IntRange): List<T>
يمكن تمرير كائن من النوع Iterable
كوسيط إلى الدالة slice()
:
fun <T> Array<out T>.slice(indices: Iterable<Int>): List<T>
fun ByteArray.slice(indices: Iterable<Int>): List<Byte>
fun ShortArray.slice(indices: Iterable<Int>): List<Short>
fun IntArray.slice(indices: Iterable<Int>): List<Int>
fun LongArray.slice(indices: Iterable<Int>): List<Long>
fun FloatArray.slice(indices: Iterable<Int>): List<Float>
fun DoubleArray.slice(indices: Iterable<Int>): List<Double>
fun BooleanArray.slice(indices: Iterable<Int>): List<Boolean>
fun CharArray.slice(indices: Iterable<Int>): List<Char>
fun <T> List<T>.slice(indices: Iterable<Int>): List<T>
القيم المٌعادة
لائحة تحتوي عناصر المصفوفة أو اللائحةالتي استُدعيت عبرها والموجودة عند الفهارس التي يحددها الوسيط الممرّر indices
.
أمثلة
استخدام الدالة ()slice
مع المصفوفات
تعرف الشيفرة الآتية مصفوفةباسم array
مكونة من ستة عناصر باستخدام الدالة ()arrayOf
، ثم تستخدم الدالة()slice
على array
مع تمرير مجال، ثم تطبع الناتج:
fun main(args: Array<String>) {
val array = arrayOf(1, 2, 3, 4, 5, 6)
print (array.slice(1..3)) // [2, 3, 4]
}
أنظر أيضًا
sliceArray()
: تعيد لائحة أو مصفوفة تحتوي عناصر المصفوفة التي استُدعيت عبرها والموجودة عند الفهارس التي يحددها الوسيط الممرّرindices
.