且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ArrayList< String>()与arrayListOf< String>()

更新时间:2022-11-24 20:13:24

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.