Java eqivalent method of “splice(a,b,…)” in JavaScript method

后端 未结 6 965
伪装坚强ぢ
伪装坚强ぢ 2021-01-20 01:22

someArray.splice(a,b,...) method in JavaScript adds or removes items to/from array. What could be good and simple solution to implement such method in Java lang

6条回答
  •  我在风中等你
    2021-01-20 01:53

    I misread your question and mixed up splice and slice.

    The class java.util.Arrays provides some static functions useful when working with arrays. See the official documentation for other functions: http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html.

    Java's equivalent for slice is: Arrays.copyOfRange(array, from, to).

    A similar method to splice is addAll (http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#addAll-int-java.util.Collection-). But you need to use a java.util.ArrayList instead an array and it is not possible to remove elements with it. You would have to provide the elements as another collection, e.g. an ArrayList. So it is equivalent to calling splice(index, 0, element1, element2, ...)

提交回复
热议问题