Insert a new element in a specified position of a list

前端 未结 3 1134
猫巷女王i
猫巷女王i 2020-12-14 22:17

There is no built-in function or a method of a List that would allow user to add a new element in a certain position of a List. I\'ve wrote a function that does this but I\'

3条回答
  •  温柔的废话
    2020-12-14 22:55

    You can also use xs.patch(i, ys, r), which replaces r elements of xs starting with i by the patch ys, by using r=0 and by making ys a singleton:

    List(1, 2, 3, 5).patch(3, List(4), 0)
    

提交回复
热议问题