當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Kotlin mutableListOf()用法及代碼示例


科特林,mutableListOf()方法用於實例化MutableList接口。 MutableList 類用於創建可變列表,可以在其中添加或刪除元素。方法mutableListOf()返回 MutableList 接口的實例,並采用特定類型或混合(取決於 MutableList 實例的類型)元素的數組,也可以為 null。

用法:

 fun  <T> mutableListOf( vararg elements: T): MutableList <T>

參數:
它采用特定類型或混合類型或空參數的數組。當需要創建 MutableList 的空實例時,使用空參數。

返回:

它返回MutableList接口的實例。

Kotlin 程序演示mutableListOf() -


fun main(args: Array<String>) 
    { 
        //declaring a mutable list of integers 
        val mutableListA = mutableListOf<Int>( 1 , 2 , 3 , 4 , 3); 
        println(mutableListA) 
  
        //declaring a mutable list of strings 
        val mutableListB = mutableListOf<String>("Geeks","for" , "geeks"); 
        println(mutableListB) 
  
        //declaring an empty mutable list of integers 
        val mutableListC = mutableSetOf<Int>() 
        println("Empty list "+mutableListC ) 
    } 

輸出:

[1, 2, 3, 4, 3]
[Geeks, for, geeks]
Empty list []

添加和刪除列表中的元素 -

我們可以使用add()函數在可變列表中添加元素,並使用remove()函數刪除元素。

Kotlin 程序演示mutableListOf() -


fun main(args: Array<String>) { 
    var mutablelist=mutableListOf("Geeks", "For"); 
    //adding string elements 
    mutablelist.add("Geeks") 
    for(i in mutablelist) 
        println(i) 
    println("... after removing \"For\" ...") 
    //removing "For" 
    mutablelist.remove("For") 
    for(i in mutablelist) 
        println(i) 
} 

輸出:

Geeks
For
Geeks
... after removing "For" ...
Geeks
Geeks

設置索引 -

使用索引函數indexOf()、lastIndexOf()我們可以獲取指定元素的索引。我們還可以使用elementAt()函數查找某個特定索引處的元素。

使用索引的 Kotlin 程序 -


fun main(args: Array<String>) 
{ 
    val captains = mutableListOf("Kohli","Smith","Root","Rohit","Dhawan") 
  
    println("The element at index 2 is: "+captains.elementAt(2)) 
  
    println("The index of element is: "+captains.indexOf("Smith")) 
  
    println("The last index of element is: "+captains.lastIndexOf("Rohit")) 
} 

輸出:

The element at index 2 is: Root
The index of element is: 1
The last index of element is: 3

列出第一個和最後一個元素 -

我們可以使用 first() 和 last() 函數獲取列表的第一個和元素。


fun main(args: Array<String>){ 
    val captains = mutableListOf(1,2,3,4,"Kohli","Smith", 
        "Root","Malinga","Dhawan","Rohit") 
  
    println("The first element of the list is: "+captains.first()) 
  
    println("The last element of the list is: "+captains.last()) 
} 

輸出:

The first element of the list is: 1
The last element of the list is: Rohit

遍曆 mutableList -

我們可以使用迭代器運行 for 循環,遍曆列表中的所有項目。


fun main(args: Array<String>) 
{ 
    //declaring a mutable list of integers 
    val seta = mutableListOf( 1 , 2 , 3 , 4 ); 
  
    //traversal of seta using an iterator 'item' 
    for(item in seta) 
        println( item ) 
} 

輸出:

1
2
3
4

contains() 和 containsAll() 函數 -

這兩種方法都用於檢查列表中是否存在元素?

使用contains()和containsAll()函數的Kotlin程序 -


fun main(args: Array<String>){ 
    val captains = mutableListOf(1,2,3,4,"Kohli","Smith", 
        "Root","Malinga","Rohit","Dhawan") 
  
  
    var name = "Dhawan"
    println("The list contains the element $name or not?" + 
            "   "+captains.contains(name)) 
  
    var num = 5
    println("The list contains the element $num or not?" + 
            "   "+captains.contains(num)) 
  
    println("The list contains the given elements or not?" + 
            "   "+captains.containsAll(setOf(1,3,"Root"))) 
} 

輸出:

The list contains the element Dhawan or not?   true
The list contains the element 5 or not?   false
The list contains the given elements or not?   true


相關用法


注:本文由純淨天空篩選整理自piyush25pv大神的英文原創作品 Kotlin mutableListOf()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。