← Back to Kotlin Course | Chapter 8: Collections | Lesson 7 of 7

Spread Operator क्या है

Spread operator आपको किसी मौजूदा array के हर item को सीधे किसी ऐसी function call में उड़ेलने देता है जो उन्हें एक-एक करके उम्मीद करती है।
Syntax
kotlin
fun functionName(vararg items: Type) { }

functionName(a, b, c)
functionName(*array)              // spread an array
functionName(*list.toTypedArray())

Declaring a vararg Function

vararg चिह्नित एक parameter उस type के किसी भी संख्या में individual arguments accept करता है, जो function के अंदर एक array के रूप में उपलब्ध होते हैं।

उदाहरण: Declaring a vararg Function

markup
// Define the function `sumAll` taking `numbers` and returning an Int
fun sumAll(vararg numbers: Int): Int {
    // Return `numbers.sum()` from this function
    return numbers.sum()
}

// Entry point: execution of the program starts here
fun main() {
    // Print "Sum: ${sumAll(1, 2, 3, 4)}" to the console, with a trailing newline
    println("Sum: ${sumAll(1, 2, 3, 4)}")
}

Spreading an Array into vararg

जब आपके पास पहले से एक array हो और आप इसके contents को individual arguments के रूप में किसी vararg parameter को पास करना चाहें, इसे * से prefix करें -- यह spread operator है।

उदाहरण: Spreading an Array into vararg

markup
// Define the function `sumAll` taking `numbers` and returning an Int
fun sumAll(vararg numbers: Int): Int {
    // Return `numbers.sum()` from this function
    return numbers.sum()
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `values`, set to `intArrayOf(10, 20, 30)`
    val values = intArrayOf(10, 20, 30)
    // Print "Sum: ${sumAll(*values)}" to the console, with a trailing newline
    println("Sum: ${sumAll(*values)}")
}

Spreading a List

चूंकि spread operator arrays पर काम करता है, किसी List को किसी vararg parameter में फैलाने से पहले पहले .toTypedArray() से convert करना चाहिए।

उदाहरण: Spreading a List

markup
// Define the function `joinAll` taking `words` and returning a String
fun joinAll(vararg words: String): String {
    // Return `words.joinToString(" ")` from this function
    return words.joinToString(" ")
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `wordList`, set to `listOf("Hello", "from", "Kotlin")`
    val wordList = listOf("Hello", "from", "Kotlin")
    // Print `joinAll(*wordList.toTypedArray())` to the console, with a trailing newline
    println(joinAll(*wordList.toTypedArray()))
}

Combining Spread with Other Arguments

एक spread array को उसी call में अतिरिक्त individual arguments के साथ मिलाया जा सकता है, जब तक overall argument order अब भी function की उम्मीद से मेल खाए।

उदाहरण: Combining Spread with Other Arguments

markup
// Define the function `sumAll` taking `numbers` and returning an Int
fun sumAll(vararg numbers: Int): Int {
    // Return `numbers.sum()` from this function
    return numbers.sum()
}

// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `values`, set to `intArrayOf(1, 2, 3)`
    val values = intArrayOf(1, 2, 3)
    // Print "Sum: ${sumAll(0, *values, 100)}" to the console, with a trailing newline
    println("Sum: ${sumAll(0, *values, 100)}")
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. किसी array को vararg parameter पास करते समय * prefix भूल जाना, एक type mismatch का कारण बनते हुए क्योंकि एक single array कई individual arguments जैसा नहीं है।
  2. किसी List को सीधे फैलाने की कोशिश करना; spread operator arrays पर काम करता है, इसलिए किसी List को पहले .toTypedArray() से convert करना चाहिए।
  3. यह मान लेना कि spread को गलत order वाले दूसरे individual arguments के साथ मिलाया जा सकता है; किसी spread से पहले/बाद के अतिरिक्त positional arguments को अब भी parameter order rules का सम्मान करना चाहिए।
चैप्टर सारांश
  • * (spread) operator किसी array को vararg parameter के लिए individual arguments में unpack करता है।
  • * के बिना, किसी array को सीधे vararg parameter पास करना एक type mismatch है।
  • किसी List को फैलाने से पहले पहले .toTypedArray() से एक array में convert करना चाहिए।
  • Spread arguments को उसी call में अतिरिक्त individual arguments के साथ combine किया जा सकता है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.