JavaScript 方法中 "splice(a,b,...)" 的 Java 等效方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29052240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java eqivalent method of "splice(a,b,...)" in JavaScript method
提问by Ernestas Gruodis
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 language? Assume we have String[]
array.
someArray.splice(a,b,...)
JavaScript 中的方法向/从数组添加或删除项目。用 Java 语言实现这种方法有什么好的和简单的解决方案?假设我们有String[]
数组。
采纳答案by Seva Safris
Here is a Java implementation of the Array.prototype.splice()
method as per the JavaScript MDN specification.
这是Array.prototype.splice()
根据JavaScript MDN 规范的方法的 Java 实现。
public static <T>T[] splice(final T[] array, int start) {
if (start < 0)
start += array.length;
return splice(array, start, array.length - start);
}
@SuppressWarnings("unchecked")
public static <T>T[] splice(final T[] array, int start, final int deleteCount) {
if (start < 0)
start += array.length;
final T[] spliced = (T[])Array.newInstance(array.getClass().getComponentType(), array.length - deleteCount);
if (start != 0)
System.arraycopy(array, 0, spliced, 0, start);
if (start + deleteCount != array.length)
System.arraycopy(array, start + deleteCount, spliced, start, array.length - start - deleteCount);
return spliced;
}
@SuppressWarnings("unchecked")
public static <T>T[] splice(final T[] array, int start, final int deleteCount, final T ... items) {
if (start < 0)
start += array.length;
final T[] spliced = (T[])Array.newInstance(array.getClass().getComponentType(), array.length - deleteCount + items.length);
if (start != 0)
System.arraycopy(array, 0, spliced, 0, start);
if (items.length > 0)
System.arraycopy(items, 0, spliced, start, items.length);
if (start + deleteCount != array.length)
System.arraycopy(array, start + deleteCount, spliced, start + items.length, array.length - start - deleteCount);
return spliced;
}
The following JUnit code tests this implementation:
以下 JUnit 代码测试此实现:
@Test
public void testSplice() {
final String[] array = new String[] {"a", "b", "c", "d", "e", "f"};
Assert.assertArrayEquals(new String[] {"c", "d", "e", "f"}, Arrays.splice(array, 0, 2));
Assert.assertArrayEquals(new String[] {"a", "d", "e", "f"}, Arrays.splice(array, 1, 2));
Assert.assertArrayEquals(new String[] {"a", "b", "e", "f"}, Arrays.splice(array, 2, 2));
Assert.assertArrayEquals(new String[] {"a", "b", "c", "f"}, Arrays.splice(array, 3, 2));
Assert.assertArrayEquals(new String[] {"a", "b", "c", "d"}, Arrays.splice(array, 4, 2));
try {
Arrays.splice(array, 5, 2);
Assert.fail("Expected ArrayIndexOutOfBoundsException");
}
catch (final ArrayIndexOutOfBoundsException e) {
}
try {
Arrays.splice(array, -2, 3);
Assert.fail("Expected ArrayIndexOutOfBoundsException");
}
catch (final ArrayIndexOutOfBoundsException e) {
}
Assert.assertArrayEquals(new String[] {"a", "b", "c", "d"}, Arrays.splice(array, -2, 2));
Assert.assertArrayEquals(new String[] {"a", "b", "c", "f"}, Arrays.splice(array, -3, 2));
Assert.assertArrayEquals(new String[] {"a", "b", "e", "f"}, Arrays.splice(array, -4, 2));
Assert.assertArrayEquals(new String[] {"a", "d", "e", "f"}, Arrays.splice(array, -5, 2));
Assert.assertArrayEquals(new String[] {"c", "d", "e", "f"}, Arrays.splice(array, -6, 2));
try {
Arrays.splice(array, -7, 2);
Assert.fail("Expected ArrayIndexOutOfBoundsException");
}
catch (final ArrayIndexOutOfBoundsException e) {
}
Assert.assertArrayEquals(new String[] {}, Arrays.splice(array, 0));
Assert.assertArrayEquals(new String[] {"a"}, Arrays.splice(array, 1));
Assert.assertArrayEquals(new String[] {"a", "b"}, Arrays.splice(array, 2));
Assert.assertArrayEquals(new String[] {"a", "b", "c"}, Arrays.splice(array, 3));
Assert.assertArrayEquals(new String[] {"a", "b", "c", "d"}, Arrays.splice(array, 4));
Assert.assertArrayEquals(new String[] {"a", "b", "c", "d", "e"}, Arrays.splice(array, 5));
Assert.assertArrayEquals(new String[] {"a", "b", "c", "d", "e", "f"}, Arrays.splice(array, 6));
try {
Arrays.splice(array, 7);
Assert.fail("Expected ArrayIndexOutOfBoundsException");
}
catch (final ArrayIndexOutOfBoundsException e) {
}
Assert.assertArrayEquals(new String[] {"a", "b", "c", "d", "e"}, Arrays.splice(array, -1));
Assert.assertArrayEquals(new String[] {"a", "b", "c", "d"}, Arrays.splice(array, -2));
Assert.assertArrayEquals(new String[] {"a", "b", "c"}, Arrays.splice(array, -3));
Assert.assertArrayEquals(new String[] {"a", "b"}, Arrays.splice(array, -4));
Assert.assertArrayEquals(new String[] {"a"}, Arrays.splice(array, -5));
Assert.assertArrayEquals(new String[] {}, Arrays.splice(array, -6));
try {
Arrays.splice(array, -7);
Assert.fail("Expected NegativeArraySizeException");
}
catch (final NegativeArraySizeException e) {
}
Assert.assertArrayEquals(new String[] {"x", "y", "z", "c", "d", "e", "f"}, Arrays.splice(array, 0, 2, "x", "y", "z"));
Assert.assertArrayEquals(new String[] {"a", "x", "y", "z", "d", "e", "f"}, Arrays.splice(array, 1, 2, "x", "y", "z"));
Assert.assertArrayEquals(new String[] {"a", "b", "x", "y", "z", "e", "f"}, Arrays.splice(array, 2, 2, "x", "y", "z"));
Assert.assertArrayEquals(new String[] {"a", "b", "c", "x", "y", "z", "f"}, Arrays.splice(array, 3, 2, "x", "y", "z"));
Assert.assertArrayEquals(new String[] {"a", "b", "c", "d", "x", "y", "z"}, Arrays.splice(array, 4, 2, "x", "y", "z"));
try {
Arrays.splice(array, 5, 2, "x", "y", "z");
Assert.fail("Expected ArrayIndexOutOfBoundsException");
}
catch (final ArrayIndexOutOfBoundsException e) {
}
Assert.assertArrayEquals(new String[] {"a", "b", "c", "d", "x", "y", "z"}, Arrays.splice(array, -2, 2, "x", "y", "z"));
Assert.assertArrayEquals(new String[] {"a", "b", "c", "x", "y", "z", "f"}, Arrays.splice(array, -3, 2, "x", "y", "z"));
Assert.assertArrayEquals(new String[] {"a", "b", "x", "y", "z", "e", "f"}, Arrays.splice(array, -4, 2, "x", "y", "z"));
Assert.assertArrayEquals(new String[] {"a", "x", "y", "z", "d", "e", "f"}, Arrays.splice(array, -5, 2, "x", "y", "z"));
Assert.assertArrayEquals(new String[] {"x", "y", "z", "c", "d", "e", "f"}, Arrays.splice(array, -6, 2, "x", "y", "z"));
try {
Arrays.splice(array, -7, 2, "x", "y", "z");
Assert.fail("Expected ArrayIndexOutOfBoundsException");
}
catch (final ArrayIndexOutOfBoundsException e) {
}
}
Edit:As @denys-séguret pointed out correctly, this implementation differs from the JavaScript spec as it does not mutate/modify the original array. Instead, this implementation returns a new array instance.
编辑:正如@denys-séguret 正确指出的那样,此实现与 JavaScript 规范不同,因为它不会改变/修改原始数组。相反,此实现返回一个新的数组实例。
Edit:This implementation is available with the following maven artifact, at the given maven repo:
编辑:在给定的 maven repo 中,此实现可用于以下 maven 工件:
<project>
...
<dependencies>
<dependency>
<groupId>org.safris.commons</groupId>
<artifactId>commons-lang</artifactId>
<version>1.6.4</version>
</dependency>
</dependencies>
...
<repositories>
<repository>
<id>mvn.repo.safris.org</id>
<url>http://mvn.repo.safris.org/m2</url>
</repository>
</repositories>
...
</project>
回答by Denys Séguret
Java arrays have a fixed length, so there's no such method.
Java 数组有固定长度,所以没有这样的方法。
You could imagine writing a utility function similar to splice in Java but it would return a different array. There's no point in having arrays in java if you resize them: it's not efficient and you can't share the instance.
您可以想象在 Java 中编写一个类似于 splice 的实用程序函数,但它会返回一个不同的数组。如果您调整数组的大小,那么在 Java 中使用数组是没有意义的:它效率不高,并且您无法共享实例。
The usual and clean solution is to use a List, which is a resizeable collection. ArrayList, the most commonly used List implementation is backed by an array but is efficient as the array isn't changed every time you resize the collection.
通常且干净的解决方案是使用List,它是一个可调整大小的集合。ArrayList是最常用的 List 实现,它由数组支持,但效率很高,因为每次调整集合大小时数组都不会改变。
回答by Crazyjavahacking
In standard Java libraries, there is no equivalent functionality.
在标准 Java 库中,没有等效的功能。
There is java.util.Arrays
class, but no similar functionality there.
有java.util.Arrays
类,但没有类似的功能。
回答by DenisG
I misread your question and mixed up splice
and slice
.
我误读了您的问题并混淆了splice
和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.util.Arrays
提供了一些在处理数组时有用的静态函数。其他功能参见官方文档:http: //docs.oracle.com/javase/8/docs/api/java/util/Arrays.html。
Java's equivalent for slice
is: Arrays.copyOfRange(array, from, to)
.
Java 的等价物slice
是: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, ...)
类似的方法splice
是addAll
(http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#addAll-int-java.util.Collection-)。但是您需要使用 ajava.util.ArrayList
代替数组,并且无法使用它删除元素。您必须将元素作为另一个集合提供,例如ArrayList
. 所以相当于调用splice(index, 0, element1, element2, ...)
回答by JonasCz - Reinstate Monica
Java arrays have a fixed length, so this cannot be done directly.
Java 数组的长度是固定的,因此不能直接完成。
If you want to combine two arrays, look at this answer.
如果要组合两个数组,请查看此答案。
If you want to add to the array, you should use a List
or an ArrayList
instead.
如果要添加到数组中,则应使用 aList
或 anArrayList
代替。
回答by lincoln pepper
Arrays in Java have a fixed number of elements. But you can make that element null like this:
Java 中的数组具有固定数量的元素。但是您可以像这样将该元素设为 null:
array[element]==null;
数组[元素]==空;
And that is the same as removing it from the array. You can also have a variable that keeps track of how many elements aren't null, so that you can even have an array.length kind of thing that follows that. that's what I do anyway.
这与从数组中删除它是一样的。你也可以有一个变量来跟踪有多少元素不是空的,这样你甚至可以有一个 array.length 紧随其后的东西。无论如何,这就是我所做的。