Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Kotlin的while循环中包括赋值和赋值测试_Kotlin_Idioms - Fatal编程技术网

在Kotlin的while循环中包括赋值和赋值测试

在Kotlin的while循环中包括赋值和赋值测试,kotlin,idioms,Kotlin,Idioms,我希望使用Java中常用的标准while循环习惯用法查找文本文件的最后一行 我有一个不太紧凑的版本。但是我想使用的那个在Kotlin中似乎不是有效的语法。我的首选方法包括一个赋值和在同一行中对该赋值进行布尔测试 诚然,这是一件小事,但我希望更好地实现我的Kotlin代码 fun readLastLine(file:File):String { val bufferedReader = file.bufferedReader() var lastLine="" //val

我希望使用Java中常用的标准while循环习惯用法查找文本文件的最后一行

我有一个不太紧凑的版本。但是我想使用的那个在Kotlin中似乎不是有效的语法。我的首选方法包括一个赋值和在同一行中对该赋值进行布尔测试

诚然,这是一件小事,但我希望更好地实现我的Kotlin代码

fun readLastLine(file:File):String {
    val bufferedReader = file.bufferedReader()
    var lastLine=""

    //valid
    var current = bufferedReader.readLine()
    while(current != null) {
        lastLine=current
        current = bufferedReader.readLine()
    }
    //return lastLine

    //not valid...
    //while((current=bufferedReader.readLine())!=null){
    //    lastLine=current
    //}

   //responding to comment below, 
   //preferred/terse answer using file.readLines
   //this reads all the lines into a list, then returns the last
   return file.readLines().last()
}

在Kotlin中,赋值不是值等于赋值的表达式

可以使用Kotlin中的run函数组合两条语句。此函数返回最后一个表达式的值

var current = ""
while (run {
    current = bufferedReader.readLine()
    current != null
}) { // or while (run { current = bufferedReader.readLine(); current != null }) {
    lastLine = current
}
但是,您可以使用Kotlin中的File.forEachLine进一步减少代码

fun readLastLine(file: File): String {
    var lastLine = ""
    file.forEachLine { line ->
        lastLine = line
    }
    return lastLine
}
或者更短

fun readLastLine(file: File): String {
    var lastLine = ""
    file.forEachLine { lastLine = it }
    return lastLine
}

它使用BufferedReader并在内部自动关闭:

在Kotlin中,赋值不是值等于赋值的表达式

可以使用Kotlin中的run函数组合两条语句。此函数返回最后一个表达式的值

var current = ""
while (run {
    current = bufferedReader.readLine()
    current != null
}) { // or while (run { current = bufferedReader.readLine(); current != null }) {
    lastLine = current
}
但是,您可以使用Kotlin中的File.forEachLine进一步减少代码

fun readLastLine(file: File): String {
    var lastLine = ""
    file.forEachLine { line ->
        lastLine = line
    }
    return lastLine
}
或者更短

fun readLastLine(file: File): String {
    var lastLine = ""
    file.forEachLine { lastLine = it }
    return lastLine
}

它使用BufferedReader并在内部自动关闭:

我知道您的兴趣纯粹是学术性质的。但是,如果有人来找你的示例问题的实现,我建议你提到使用file.readLines.lastFYI的惯用解决方案,我遇到了一个表达式中关于赋值的讨论。这似乎是Kotlin语言的合理设计问题,设计师已经回答了这个问题:我知道你的兴趣纯粹是学术性质的。但是,如果有人来找你的示例问题的实现,我建议你提到使用file.readLines.lastFYI的惯用解决方案,我遇到了一个表达式中关于赋值的讨论。这似乎是Kotlin语言的合理设计问题,设计师已经回答了这个问题: