我正在研究Kotlin的一些基础知识,发现了两种语法.
I was going through some Kotlin basics and found two syntaxes.
ArrayList<String>()还有
arrayListOf<String>()这两者是Kotlin.Collections的一部分,有什么区别?
What is the difference between these two as both are part of Kotlin.Collections?
推荐答案arrayListOf<T>() 主要是为了您的方便. vararg函数通常会对性能产生影响(有时可以忽略不计),并且在arrayListOf(someElements...)和arrayListOf()之间进行切换时,如果没有这种便捷方法,基本上就可以将该问题委托给您作为程序员.您必须知道它,并且必须将代码更改为 ArrayList() ,如果这种影响正在影响您,并且该便利功能将不存在.
arrayListOf<T>() is mainly there for your convenience. vararg-functions usually come with a (sometimes negligible) performance impact and switching between the arrayListOf(someElements...) and arrayListOf() without that convenience method would basically delegate that problem to you as a programmer. You have to know it and you would have to change that code to ArrayList(), if such an impact is affecting you and if that convenience function wouldn't exist.
arrayListOf()基本上就是这样.它返回ArrayList()并被内联.这很方便,因此当您在arrayListOf(someElements)和arrayListOf()之间来回切换时,您不必真正考虑它.
arrayListOf() is basically just that. It returns ArrayList() and it is inlined. That's just convenient, so that you don't really have to think about it, when you switch back and forth between arrayListOf(someElements) and arrayListOf().
话虽如此:arrayListOf()和ArrayList()之间没有区别,正如其他人已经提到的一样,arrayListOf(elements)是使用给定元素构造ArrayList的便利变量.
That having said: there is no difference between arrayListOf() and ArrayList() as also others have already mentioned and arrayListOf(elements) is the convenience variant to construct an ArrayList with the given elements.
更多推荐
ArrayList< String>()与arrayListOf< String>()
发布评论