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

编程入门 行业动态 更新时间:2024-10-21 18:43:39
本文介绍了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>() 主要是为了您的方便. 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&lt; String&gt;()与arrayListOf&lt; String&gt;()

本文发布于:2023-11-02 17:31:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552983.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:lt   amp   ArrayList   arrayListOf   gt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!