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

MutableList क्या है

एक MutableList एक List है जिसे आपको बनने के बाद बदलते रहने की अनुमति है, जब चाहें items जोड़ते और हटाते हुए।
Syntax
kotlin
val list = mutableListOf(a, b, c)
list.add(item)
list.add(index, item)
list.remove(item)
list.removeAt(index)
list[index] = newValue

Creating a MutableList

mutableListOf(...) एक list बनाता है जो listOf() की read-only List के विपरीत बनने के बाद elements जोड़ने, हटाने, और replace करने को support करता है।

उदाहरण: Creating a MutableList

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `numbers`, set to `mutableListOf(1, 2, 3)`
    val numbers = mutableListOf(1, 2, 3)
    // Print `numbers` to the console, with a trailing newline
    println(numbers)
}

Adding Elements

.add(item) list के आखिर में एक नया element जोड़ता है, जबकि .add(index, item) इसे किसी specific position पर insert करता है, बाद के elements को आगे खिसकाते हुए।

उदाहरण: Adding Elements

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `numbers`, set to `mutableListOf(1, 2, 3)`
    val numbers = mutableListOf(1, 2, 3)
    // Add `4` to `numbers`
    numbers.add(4)
    // Add `0, 0` to `numbers`
    numbers.add(0, 0)
    // Print `numbers` to the console, with a trailing newline
    println(numbers)
}

Removing Elements

.remove(value) value से मेल खाता पहला element हटाता है, जबकि .removeAt(index) किसी specific position पर element हटाता है।

उदाहरण: Removing Elements

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `numbers`, set to `mutableListOf(10, 20, 30, 40)`
    val numbers = mutableListOf(10, 20, 30, 40)
    numbers.remove(20)
    numbers.removeAt(0)
    // Print `numbers` to the console, with a trailing newline
    println(numbers)
}

Updating Elements by Index

list[index] = value को assign करना उस position पर element को सीधे replace कर देता है, बिना हटाने और फिर जोड़ने की ज़रूरत के।

Note: किसी index के साथ iterate करना और index से modify करना (structurally नहीं) सुरक्षित है; सीधे loop करते हुए जोड़ना/हटाना नहीं है।

उदाहरण: Updating Elements by Index

markup
// Entry point: execution of the program starts here
fun main() {
    // Declare a read-only variable `numbers`, set to `mutableListOf(1, 2, 3)`
    val numbers = mutableListOf(1, 2, 3)
    numbers[1] = 99
    // Print `numbers` to the console, with a trailing newline
    println(numbers)
}
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. mutableListOf() का उपयोग करना जब ऐसे data के लिए एक साधारण read-only listOf() ज़्यादा सुरक्षित और साफ़ होता जिसे असल में कभी बदलने की ज़रूरत नहीं।
  2. किसी MutableList को for loop से सीधे iterate करते हुए modify करना, एक ConcurrentModificationException का जोखिम उठाते हुए।
  3. यह भूल जाना कि removeAt() एक index लेता है जबकि remove() एक value लेता है, और गलती से गलत वाले का उपयोग करना।
चैप्टर सारांश
  • mutableListOf(...) एक MutableList बनाता है, .add(), .remove(), और .removeAt() को support करते हुए।
  • .add(item) आखिर में एक item जोड़ता है; .add(index, item) किसी specific position पर insert करता है।
  • .remove(item) किसी value का पहला occurrence हटाता है; .removeAt(index) position से हटाता है।
  • किसी mutable collection को for loop से सीधे iterate करते हुए modify करना एक ConcurrentModificationException फेंक सकता है।
🔒

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.