ArrayList< String>()与arrayListOf< String>() [英] ArrayList<String>() vs arrayListOf<String>()

查看:95
本文介绍了ArrayList< String>()与arrayListOf< String>()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究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>() 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&lt; String&gt;()与arrayListOf&lt; String&gt;()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆