List के Basics
In this page:
val list = listOf(a, b, c) // read-only
list[0]
list.size
list.first()
list.last()
list.isEmpty()
Creating a List
listOf(...) दिए गए order में दिए गए elements वाली एक read-only List बनाता है, जो values के एक fixed collection के साथ काम करने का सबसे आम तरीका है।
उदाहरण: Creating a List
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `fruits`, set to `listOf("apple", "banana", "cherry")`
val fruits = listOf("apple", "banana", "cherry")
// Print `fruits` to the console, with a trailing newline
println(fruits)
}
Login to try C/C++/Java/PHP code in the editor
Accessing Elements by Index
Elements को एक zero-based index के साथ square brackets उपयोग करके निकाला जाता है, जैसे पहले element के लिए fruits[0]।
उदाहरण: Accessing Elements by Index
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `fruits`, set to `listOf("apple", "banana", "cherry")`
val fruits = listOf("apple", "banana", "cherry")
// Print "First: ${fruits[0]}" to the console, with a trailing newline
println("First: ${fruits[0]}")
// Print "Last: ${fruits[fruits.size - 1]}" to the console, with a trailing newline
println("Last: ${fruits[fruits.size - 1]}")
}
Login to try C/C++/Java/PHP code in the editor
Common List Properties and Functions
List बिना mutate किए collection को inspect करने के लिए .size, .first(), .last(), .isEmpty(), और .contains(item) जैसे सुविधाजनक read-only members प्रदान करता है।
उदाहरण: Common List Properties and Functions
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `fruits`, set to `listOf("apple", "banana", "cherry")`
val fruits = listOf("apple", "banana", "cherry")
// Print "Size: ${fruits.size}" to the console, with a trailing newline
println("Size: ${fruits.size}")
// Print "First: ${fruits.first()}" to the console, with a trailing newline
println("First: ${fruits.first()}")
// Print "Contains banana: ${fruits.contains("banana")}" to the console, with a trailing newline
println("Contains banana: ${fruits.contains("banana")}")
}
Login to try C/C++/Java/PHP code in the editor
Lists Are Read-Only, Not Necessarily Immutable
एक List reference खुद elements जोड़ या हटा नहीं सकता, लेकिन अगर यह कहीं और एक MutableList reference जैसे ही object को point करे, तो उस दूसरे reference के ज़रिए किए बदलाव अब भी दिखाई देते हैं।
List को immutable के बजाय read-only कहा जाता है।उदाहरण: Lists Are Read-Only, Not Necessarily Immutable
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `mutable`, set to `mutableListOf("a", "b")`
val mutable = mutableListOf("a", "b")
// Declare a read-only variable `readOnlyView` (a `List<String>`), set to `mutable`
val readOnlyView: List<String> = mutable
// Add `"c"` to `mutable`
mutable.add("c")
// Print `readOnlyView` to the console, with a trailing newline
println(readOnlyView)
}
Login to try C/C++/Java/PHP code in the editor
listOf()से बनी किसीListपर.add()call करने की कोशिश करना, यह भूलते हुए कि यह read-only है और ज़रूरी नहीं कि mutable हो।- यह मान लेना कि एक read-only
Listकभी नहीं बदल सकती; अगर इसकी underlying value एकMutableListहै, तो उस reference के ज़रिए किए बदलाव read-only view के ज़रिए भी दिखाई देते हैं। - bounds जांचे बिना index-based access उपयोग करना, किसी invalid index पर
IndexOutOfBoundsExceptionका कारण बनते हुए।
listOf(...)एक read-onlyListबनाता है -- आप इसके ज़रिए elements जोड़, हटा, या replace नहीं कर सकते।- Elements को
list[index]याlist.get(index)का उपयोग करके zero-based index से access किया जाता है। List.size,.first(),.last(), और.contains()जैसे कई read-only helper functions प्रदान करता है।- अगर उसी underlying collection का कोई दूसरा reference mutable हो तो एक read-only
Listtype असली immutability की गारंटी नहीं देता।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: