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

List के Basics

एक List items की एक ordered row है जिसे आप देख सकते हैं, लेकिन listOf से बनाने के बाद बदल नहीं सकते।
Syntax
kotlin
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

markup
// 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)
}

Accessing Elements by Index

Elements को एक zero-based index के साथ square brackets उपयोग करके निकाला जाता है, जैसे पहले element के लिए fruits[0]।

उदाहरण: Accessing Elements by Index

markup
// 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]}")
}

Common List Properties and Functions

List बिना mutate किए collection को inspect करने के लिए .size, .first(), .last(), .isEmpty(), और .contains(item) जैसे सुविधाजनक read-only members प्रदान करता है।

उदाहरण: Common List Properties and Functions

markup
// 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")}")
}

Lists Are Read-Only, Not Necessarily Immutable

एक List reference खुद elements जोड़ या हटा नहीं सकता, लेकिन अगर यह कहीं और एक MutableList reference जैसे ही object को point करे, तो उस दूसरे reference के ज़रिए किए बदलाव अब भी दिखाई देते हैं।

Note: यही वजह है कि Kotlin की terminology में List को immutable के बजाय read-only कहा जाता है।

उदाहरण: Lists Are Read-Only, Not Necessarily Immutable

markup
// 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)
}
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. listOf() से बनी किसी List पर .add() call करने की कोशिश करना, यह भूलते हुए कि यह read-only है और ज़रूरी नहीं कि mutable हो।
  2. यह मान लेना कि एक read-only List कभी नहीं बदल सकती; अगर इसकी underlying value एक MutableList है, तो उस reference के ज़रिए किए बदलाव read-only view के ज़रिए भी दिखाई देते हैं।
  3. bounds जांचे बिना index-based access उपयोग करना, किसी invalid index पर IndexOutOfBoundsException का कारण बनते हुए।
चैप्टर सारांश
  • listOf(...) एक read-only List बनाता है -- आप इसके ज़रिए 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 List type असली immutability की गारंटी नहीं देता।
🔒

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.