ArrayList() vs arrayListOf()

前端 未结 5 1291
无人共我
无人共我 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){
    
       val list: ArrayList = arrayListOf()
    
       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
    

提交回复
热议问题