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

من موسوعة حسوب
اذهب إلى التنقل اذهب إلى البحث
ط (مراجعة وتدقيق.)
سطر 1: سطر 1:
<noinclude>{{DISPLAYTITLE: الدالة <code>slice()‎</code> في لغة Kotlin}}</noinclude>
+
<noinclude>{{DISPLAYTITLE: الدالة <code>slice()‎</code> في Kotlin}}</noinclude>
تعيد الدالة<code>slice()‎</code>[[Kotlin/List|لائحة]] تحتوي عناصر [[Kotlin/Array|المصفوفة]] أو [[Kotlin/List|اللائحة]] التي استُدعيت عبرها والموجودة عند الفهارس التي يحددها الوسيط الممرّر <code>indices</code>.  
+
تقطع الدالة <code>slice()‎</code> جزءًا محدَّدًا من [[Kotlin/Array|المصفوفة]] أو [[Kotlin/List|القائمة]] التي استُدعيت معها ثمَّ تعيده في قائمة.  
  
 
==البنية العامة==
 
==البنية العامة==
يمكن تمرير [[Kotlin/ranges|مجال]] كوسيط إلى الدالة <code>slice()‎</code>:<nowiki/><nowiki/><syntaxhighlight lang="kotlin">
+
يمكن تمرير [[Kotlin/ranges|مجال]] محدَّد إلى الدالة <code>slice()‎</code> كوسيط:<nowiki/><nowiki/><syntaxhighlight lang="kotlin">
 
fun <T> Array<out T>.slice(indices: IntRange): List<T>  
 
fun <T> Array<out T>.slice(indices: IntRange): List<T>  
 
fun ByteArray.slice(indices: IntRange): List<Byte>  
 
fun ByteArray.slice(indices: IntRange): List<Byte>  
سطر 14: سطر 14:
 
fun CharArray.slice(indices: IntRange): List<Char>  
 
fun CharArray.slice(indices: IntRange): List<Char>  
 
fun <T> List<T>.slice(indices: IntRange): List<T>  
 
fun <T> List<T>.slice(indices: IntRange): List<T>  
</syntaxhighlight>يمكن تمرير كائن من النوع <code>Iterable</code> كوسيط إلى الدالة <code>slice()‎</code>:<nowiki/><nowiki/><syntaxhighlight lang="kotlin">
+
</syntaxhighlight>أو يمكن تمرير كائن من النوع <code>Iterable</code> إلى الدالة <code>slice()‎</code> كوسيط:<nowiki/><nowiki/><syntaxhighlight lang="kotlin">
 
fun <T> Array<out T>.slice(indices: Iterable<Int>): List<T>  
 
fun <T> Array<out T>.slice(indices: Iterable<Int>): List<T>  
 
fun ByteArray.slice(indices: Iterable<Int>): List<Byte>  
 
fun ByteArray.slice(indices: Iterable<Int>): List<Byte>  
سطر 26: سطر 26:
 
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>.  
+
تُعاد [[Kotlin/List|قائمة]] تحوي جزءًا من [[Kotlin/Array|المصفوفة]] أو [[Kotlin/List|القائمة]] <nowiki/>المعطاة يحدِّده الوسيط <code>indices</code> المعطى.  
 
==أمثلة==
 
==أمثلة==
===استخدام الدالة <code>()slice</code> مع المصفوفات===
+
<nowiki/><nowiki/>استعمال الدالة <code>()slice</code> لجلب جزء من مصفوفة يقع بين العنصر الثاني والخامس:<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 (array.slice(1..3)) // [2, 3, 4]
+
     print (array.slice(1..4)) // [2, 3, 4, 5]
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
===أنظر أيضًا===
+
==انظر أيضًا==
*<code>[[Kotlin/collections/sliceArray|sliceArray()]]</code>‎ : تعيد [[Kotlin/List|لائحة]] أو [[Kotlin/Array|مصفوفة]] تحتوي عناصر [[Kotlin/Array|المصفوفة]] التي استُدعيت عبرها والموجودة عند الفهارس التي يحددها الوسيط الممرّر <code>indices</code>. 
+
*الدالة <code>[[Kotlin/collections/sliceArray|sliceArray()]]</code>‎ : تقطع جزءًا محدَّدًا من [[Kotlin/Array|المصفوفة]] التي استُدعيت معها ثمَّ تعيده في مصفوفة أو قائمة بحسب الوسيط الممرر إليها.
 +
* التابع <code>[[Kotlin/Array/get|Array.get()‎]]</code>: تجلب قيمة العنصر في المصفوفة عند الفهرس المُحدَّد.
 +
* الدالة <code>[[Kotlin/collections/indexOf|indexOf()‎]]</code>: تجلب فهرس أول ظهور لعنصر محدَّد من <nowiki/>[[Kotlin/Array|المصفوفة]] أو <nowiki/>[[Kotlin/collections|المجموعة]] أو القائمة التي استُدعيت معها.
 +
* الخاصية <code>[[Kotlin/collections/indices|‎‎indices]]</code>: تجلب الفهارس الصحيحة (valid indices) للمصفوفة أو المجموعة (collection).
 
==مصادر==
 
==مصادر==
*[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]]

مراجعة 16:53، 7 أغسطس 2018

تقطع الدالة slice()‎ جزءًا محدَّدًا من المصفوفة أو القائمة التي استُدعيت معها ثمَّ تعيده في قائمة.

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

يمكن تمرير مجال محدَّد إلى الدالة 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 لجلب جزء من مصفوفة يقع بين العنصر الثاني والخامس:

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

    print (array.slice(1..4)) // [2, 3, 4, 5]
}

انظر أيضًا

  • الدالة sliceArray()‎ : تقطع جزءًا محدَّدًا من المصفوفة التي استُدعيت معها ثمَّ تعيده في مصفوفة أو قائمة بحسب الوسيط الممرر إليها.
  • التابع Array.get()‎: تجلب قيمة العنصر في المصفوفة عند الفهرس المُحدَّد.
  • الدالة indexOf()‎: تجلب فهرس أول ظهور لعنصر محدَّد من المصفوفة أو المجموعة أو القائمة التي استُدعيت معها.
  • الخاصية ‎‎indices: تجلب الفهارس الصحيحة (valid indices) للمصفوفة أو المجموعة (collection).

مصادر