MutableList क्या है
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
// 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)
}
Login to try C/C++/Java/PHP code in the editor
Adding Elements
.add(item) list के आखिर में एक नया element जोड़ता है, जबकि .add(index, item) इसे किसी specific position पर insert करता है, बाद के elements को आगे खिसकाते हुए।
उदाहरण: Adding Elements
// 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)
}
Login to try C/C++/Java/PHP code in the editor
Removing Elements
.remove(value) value से मेल खाता पहला element हटाता है, जबकि .removeAt(index) किसी specific position पर element हटाता है।
उदाहरण: Removing Elements
// 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)
}
Login to try C/C++/Java/PHP code in the editor
Updating Elements by Index
list[index] = value को assign करना उस position पर element को सीधे replace कर देता है, बिना हटाने और फिर जोड़ने की ज़रूरत के।
उदाहरण: Updating Elements by Index
// 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)
}
Login to try C/C++/Java/PHP code in the editor
mutableListOf()का उपयोग करना जब ऐसे data के लिए एक साधारण read-onlylistOf()ज़्यादा सुरक्षित और साफ़ होता जिसे असल में कभी बदलने की ज़रूरत नहीं।- किसी
MutableListकोforloop से सीधे iterate करते हुए modify करना, एकConcurrentModificationExceptionका जोखिम उठाते हुए। - यह भूल जाना कि
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 को
forloop से सीधे iterate करते हुए modify करना एकConcurrentModificationExceptionफेंक सकता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: