首页 文章

Kotlin的名单缺少“添加”,“删除”等?

提问于
浏览
84

在Java中,我们可以执行以下操作

public class TempClass {
    List<Integer> myList = null;
    void doSomething() {
        myList = new ArrayList<>();
        myList.add(10);
        myList.remove(10);
    }
}

但如果我们直接将其重写为Kotlin,如下所示

class TempClass {
    var myList: List<Int>? = null
    fun doSomething() {
        myList = ArrayList<Int>()
        myList!!.add(10)
        myList!!.remove(10)
    }
}

我从我的列表中找不到 addremove 函数的错误

我努力将它转换为ArrayList,但这需要投射它,而在Java中不需要 . 这就违背了抽象类List的目的

class TempClass {
    var myList: List<Int>? = null
    fun doSomething() {
        myList = ArrayList<Int>()
        (myList!! as ArrayList).add(10)
        (myList!! as ArrayList).remove(10)
    }
}

有没有办法让我使用List但不需要强制转换它,就像在Java中可以做到的那样?

7 回答

  • 3

    与许多语言不同,Kotlin区分可变集合和不可变集合(列表,集合,映射等) . 精确控制何时可以编辑集合对于消除错误和设计良好的API非常有用 .

    https://kotlinlang.org/docs/reference/collections.html

    您需要使用 MutableList 列表 .

    class TempClass {
        var myList: MutableList<Int> = mutableListOf<Int>()
        fun doSomething() {
            // myList = ArrayList<Int>() // initializer is redundant
            myList.add(10)
            myList.remove(10)
        }
    }
    

    MutableList<Int> = arrayListOf() 也应该有效 .

  • 3

    以不同方式在Kotlin中定义List集合:

    • Immutable variable with immutable (read only) list:
    val users: List<User> = listOf( User("Tom", 32), User("John", 64) )
    
    • Immutable variable with mutable list:
    val users: MutableList<User> = mutableListOf( User("Tom", 32), User("John", 64) )
    

    或没有初始值 - 空列表和没有显式变量类型:

    val users = mutableListOf<User>()
    //or
    val users = ArrayList<User>()
    
    • 您可以将项目添加到列表中:

    • users.add(anohterUser)

    • users += anotherUser (引擎盖下是 users.add(anohterUser)

    • Mutable variable with immutable list:

    var users: List<User> = listOf( User("Tom", 32), User("John", 64) )
    

    或没有初始值 - 空列表和没有显式变量类型:

    var users = emptyList<User>()
    
    • 注意:您可以将*项添加到列表中:

    • users += anotherUser - *它创建新的ArrayList并将其分配给 users

    • Mutable variable with mutable list:

    var users: MutableList<User> = mutableListOf( User("Tom", 32), User("John", 64) )
    

    或没有初始值 - 空列表和没有显式变量类型:

    var users = emptyList<User>().toMutableList()
    //or
    var users = ArrayList<User>()
    
    • 注意:您可以将项目添加到列表中:

    • users.add(anohterUser)

    • 但未使用 users += anotherUser

    错误:Kotlin:赋值运算符歧义:public operator fun Collection.plus(element:String):在kotlin.collections中定义的列表@InlineOnly public inline operator fun MutableCollection.plusAssign(element:String):在kotlin.collections中定义的单位

    另见:https://kotlinlang.org/docs/reference/collections.html

  • 15

    显然,Kotlin的默认列表是不可变的 . 要拥有可以更改的List,应该使用MutableList,如下所示

    class TempClass {
        var myList: MutableList<Int>? = null
        fun doSomething() {
            myList = ArrayList<Int>()
            myList!!.add(10)
            myList!!.remove(10)
        }
    }
    

    Updated 但是,除非您真的想要更改列表,否则不建议使用MutableList . 关于只读集合如何提供更好的编码,请参阅https://hackernoon.com/read-only-collection-in-kotlin-leads-to-better-coding-40cdfa4c6359 .

  • 165

    https://kotlinlang.org/docs/reference/collections.html

    根据以上链接,List <E>在Kotlin中是不可变的 . 不过这样可行:

    var list2 = ArrayList<String>()
    list2.removeAt(1)
    
  • 5

    同意上述所有使用MutableList的答案,但您也可以在List中添加/删除并获取如下新列表 .

    val newListWithElement = existingList + listOf(element)
    val newListMinusElement = existingList - listOf(element)
    

    要么

    val newListWithElement = existingList.plus(element)
    val newListMinusElement = existingList.minus(element)
    
  • 1

    在不可变数据的概念中,也许这是一种更好的方法:

    class TempClass {
        val list: List<Int> by lazy {
            listOf<Int>()
        }
        fun doSomething() {
            list += 10
            list -= 10
        }
    }
    
  • 1

    listimmutable by Default ,您可以使用 ArrayList 代替 . 像这样 :

    val orders = arrayListOf<String>()
    

    那么你可以 add/delete 这样的项目如下:

    orders.add("Item 1")
    orders.add("Item 2")
    

    默认情况下,ArrayList是可变的,因此您可以对其执行操作 .

相关问题