草庐IT

kotlin-stdlib-jdk

全部标签

type-conversion - Kotlin 的 Double.toInt() 中使用了哪种方法,舍入还是截断?

开启theofficialAPIdoc,它说:ReturnsthevalueofthisnumberasanInt,whichmayinvolveroundingortruncation.我想要截断,但不确定。谁能解释一下可能涉及舍入或截断的确切含义?p.s.:在我的单元测试中,(1.7).toInt()为1,可能涉及截断。 最佳答案 Double.toInt()的KDoc简单地继承自Number.toInt(),为此,确切的含义是,它在具体的Number实现中定义如何将其转换为Int。在Kotlin中,Double操作遵循IEEE

oop - Kotlin 中空类的目的是什么?

我正在使用Kotlinreferencedocument然后我看到了这个。Theclassdeclarationconsistsoftheclassname,theclassheader(specifyingitstypeparameters,theprimaryconstructoretc.)andtheclassbody,surroundedbycurlybraces.Boththeheaderandthebodyareoptional;iftheclasshasnobody,curlybracescanbeomitted.classEmpty现在我想知道没有标题和正文的类声明有什

spring - Kotlin Spring boot @ConfigurationProperties 用于列表

我想使用Kotlin读取yaml配置文件,下面是我的代码:application.ymlmessage:messages:-name:abctype:aaasize:10-name:xyztype:bbbsize:20MessageConfig.ktpackagecom.example.demokotlinimportorg.springframework.boot.context.properties.ConfigurationPropertiesimportorg.springframework.context.annotation.Configurationimportjava.

android - 来自 Transformation 的数据绑定(bind) LiveData - Android Kotlin

我正在学习kotlin和android架构组件。我有一个谷歌地图上的map切换按钮的简单用例。我想使用数据绑定(bind)将map切换按钮标签绑定(bind)到我的ViewModel中的MutableLiveData字段。我通过Activity中的onCreate方法在MapViewModel中设置mapTypeval。如果我理解正确,这应该会触发mapLabelval由于使用Transformations.map而发生变化。它不工作...为什么?这是我的版本:AndroidStudio3.2Canary4kotlin_version='1.2.21'支持="27.1.0"arch_c

kotlin - 如何在 Kotlin 的列表中创建包含每两个项目的 map ?

我正在尝试创建一个从列表中获取两个值并使用第一个值作为键和第二个键作为值的映射,但我不知道该怎么做。假设我有一个如下列表。-e、正常、-t、flat、-s,测试如何从该列表中创建如下所示的map?-e到normal,-t到flat,-s测试 最佳答案 如果你想使用内置函数,chunked可以快速做到这一点:valarguments=listOf("-e","normal","-t","flat","-s","test")valmap:Map=arguments.chunked(2){(switch,value)->switchtov

Kotlin:如何从范围返回一些值?

在Scala中我可以这样写:valsomething={valtemp1=...valtemp2=...temp1+temp2}据我所知,在Kotlin中做同样事情的最佳方法是:valsomething={valtemp1=...valtemp2=...temp1+temp2}()实际上它是一个类型为Unit->Int的lambda,它会立即被调用。我想知道这段代码可以以某种方式改进吗?也许有一个内置函数允许我写valsomething=block{...}或类似的东西? 最佳答案 你可以使用函数run,比如:valsomethin

ternary-operator - Kotlin 等价于三元运算符

这个问题在这里已经有了答案:Howtowriteternaryconditionaloperator?(33个答案)关闭6年前。所以在java中我们有三元运算符(?),它有时对简化if-else内联计算的某些值很有用。例如:myAdapter.setAdapterItems(textToSearch.length==0?noteList:noteList.sublist(0,length-5))我知道kotlin中的等价物是:myAdapter.setAdapterItems(if(textToSearch.length==0)noteListelsenoteList.sublist(

java - 如何在 Kotlin 的 Java 8 流上调用 collect(Collectors.toList())?

我有一些代码:directoryChooser.title="Selectthedirectory"valfile=directoryChooser.showDialog(null)if(file!=null){varfiles=Files.list(file.toPath()).filter{f->f.fileName.endsWith("zip")&&f.fileName.endsWith("ZIP")&&(f.fileName.startsWith("1207")||f.fileName.startsWith("4407")||f.fileName.startsWith("150

kotlin - 处理这种情况的最佳方法是 "smart cast is imposible"

我想知道处理这种情况的最佳方法是什么classPerson(varname:String?=null,varage:Int?=null){funtest(){if(name!=null&&age!=null)doSth(name,age)//smartcastimposible}fundoSth(someValue:String,someValue2:Int){}}调用doSth方法并确保name和age为ntnull的最简单方法是什么?我正在寻找一些简单的东西,比如我会简单地使用let的可变场景name?.let{doSth(it)} 最佳答案

Kotlin 使用 Java 回调接口(interface)

我有一个WebView。我想打电话publicvoidevaluateJavascript(Stringscript,ValueCallbackresultCallback)这个方法。这里是ValueCallback接口(interface):publicinterfaceValueCallback{/***Invokedwhenthevalueisavailable.*@paramvalueThevalue.*/publicvoidonReceiveValue(Tvalue);};这是我的kotlin代码:webView.evaluateJavascript("a",ValueCal