建议如何在Kotlin中创建常量?命名规则是什么?我在文档里没有找到。

companion object {
    //1
    val MY_CONST = "something"

    //2
    const val MY_CONST = "something"

    //3
    val myConst = "something"
}

或者…?

假设我有一个人的列表,我需要先按年龄排序,然后按名字排序。

来自c#背景,我可以通过使用LINQ在上述语言中轻松实现这一点:

var list=new List<Person>();
list.Add(new Person(25, "Tom"));
list.Add(new Person(25, "Dave"));
list.Add(new Person(20, "Kate"));
list.Add(new Person(20, "Alice"));

//will produce: Alice, Kate, Dave, Tom
var sortedList=list.OrderBy(person => person.Age).ThenBy(person => person.Name).ToList(); 

如何使用Kotlin来完成这个任务?

这就是我所尝试的(这显然是错误的,因为第一个“sortedBy”子句的输出被第二个子句覆盖,这导致一个列表仅按名称排序)

val sortedList = ArrayList(list.sortedBy { it.age }.sortedBy { it.name })) //wrong

突然我开始得到这个错误,我不知道为什么如果有人告诉我这个错误在哪里,就足够有帮助了。正如我所能得到的,这是因为android studio的新更新。 我得到的错误的详细总结。

Task :app:kaptDebugKotlin
    ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1ANTLR Runtime version 4.5.3 used for parser compilation does not match the current runtime version 4.7.1ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1ANTLR Runtime version 4.5.3 used for parser compilation does not match the current runtime version 4.7.1C:\Users\shubh\Downloads\MarginCalculator\app\build\generated\source\kapt\debug\com\kotlin_developer\margincalculator\DataBinderMapperImpl.java:10: error: cannot find symbol
    import com.kotlin_developer.margincalculator.databinding.FragmentCalculatorScreenBindingImpl;

    symbol:   class FragmentCalculatorScreenBindingImpl

    Task :app:kaptDebugKotlin FAILED
    location: package com.kotlin_developer.margincalculator.databinding
    FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
   > java.lang.reflect.InvocationTargetException (no error message)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 17s
29 actionable tasks: 27 executed, 2 up-to-date

我试图理解具体化关键字的目的。显然,它允许我们对泛型进行反思。

然而,当我把它去掉的时候,它仍然可以正常工作。这在什么时候会产生实际的影响?

似乎很简单,但是,我如何初始化Kotlin的MutableList空MutableList?

我可以这样破解,但我相信有更简单的方法:

var pusta: List<Kolory> = emptyList()
var cos: MutableList<Kolory> = pusta.toArrayList()

在Kotlin中var和val的区别是什么?

我已经通过了这个链接:

属性和字段

如本连结所述:

只读属性声明的完整语法不同于 可变1有两种方式:它以val开头,而不是var 不允许设置。

但在此之前有一个使用setter的例子。

fun copyAddress(address: Address): Address {
    val result = Address() // there's no 'new' keyword in Kotlin
    result.name = address.name // accessors are called
    result.street = address.street
    // ...
    return result
}

var和val的确切区别是什么?

为什么我们两者都需要?

这不是Kotlin中的变量的副本,与Java的区别:'var'和。“val”?因为我问的是与文档中的特定例子有关的疑问,而不仅仅是一般的疑问。

在Java中,数组可以这样初始化:

int numbers[] = new int[] {10, 20, 30, 40, 50}

Kotlin的数组初始化是怎样的?

我想在我的项目中实现一个时间表功能。所以我在谷歌上搜索了一个警报管理程序,但我找不到任何例子。

谁能帮我一个基本的警报管理程序?

Kotlin新手问:“为什么下面的代码不能编译?”:

var left: Node? = null
    
fun show() {
    if (left != null) {
        queue.add(left) // ERROR HERE
    }
}

智能转换为'Node'是不可能的,因为'left'是可变的 此时可以更改的属性

我得到left is mutable变量,但我显式检查left != null left是Node类型,为什么它不能被智能转换为这种类型?

我该如何优雅地解决这个问题?

我最近读了关于const关键字的文章,我很困惑!我找不到const和val关键字之间的任何区别,我的意思是我们可以用它们来创建一个不可变变量,还有什么我遗漏的吗?