ArrayList() vs arrayListOf()

前端 未结 5 1287
无人共我
无人共我 2021-01-20 01:10

I was going through some Kotlin basics and found two syntaxes.

ArrayList()

And

arrayListOf()


        
相关标签:
5条回答
  • 2021-01-20 01:20

    it a function is right but is use of like this

    here in function used set() function of arrayListOf() is used to set the given element at specified index and replace if any element already present at that index

    fun main(args: Array<String>){
    
       val list: ArrayList<String> = arrayListOf<String>()
    
       list.add("Ajay")
       list.add("Vijay")
       list.add("Prakash")
    
      println(".......print list.......")
      for (i in list) {
          println(i)
      }
      println(".......arrayList.set(2,\"Rohan\").......")
      list.set(2,"Rohan")
      println(".......print ArrayList.......")
      for (i in list) {
          println(i)
      }
    }
    

    Output

    .......print list.......
    Ajay
    Vijay
    Prakash
    .......list.set(2,"Rohan").......
    .......print list.......
    Ajay
    Vijay
    Rohan
    
    0 讨论(0)
  • 2021-01-20 01:28
    arrayListOf<T>()
    

    is just an extension function that looks like this:

    public inline fun <T> arrayListOf(): ArrayList<T> = ArrayList()
    
    0 讨论(0)
  • 2021-01-20 01:31

    arrayListOf is a function, that has optional variable length arguments

    In case of using it without arguments, there is no difference

    0 讨论(0)
  • 2021-01-20 01:40

    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() 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().

    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.

    0 讨论(0)
  • 2021-01-20 01:44

    When creating an empty array, you can use either:

    val emptyArray1 = ArrayList()
    val emptyArray2 = arrayListOf()
    

    But when creating an array from existing elements, you have to use one or the other depending on whether the existing elements are already in a collection or you want to specify them individually:

    val arrayFromCollection = ArrayListOf(hashMap.keys)
    val arrayFromElements = arrayListOf("1", "2", "3")
    

    Note that you can use the spread operator to pass an existing collection into arrayListOf as individual elements, but if the collection is anything other than another array, you also need to convert it to an array. This probably isn't worth the extra verbosity:

    val arrayFromCollectionVerbose = arrayListOf(*hashMap.keys.toTypedArray())
    
    0 讨论(0)
提交回复
热议问题