someArray.splice(a,b,...)
方法在数组中添加或删除项目。在Java语言中实现这种方法有什么好方法?假设我们有String[]
数组。
答案 0 :(得分:4)
Java数组具有固定长度,因此没有这样的方法。
您可以想象在Java中编写类似于splice的实用程序函数,但它会返回不同的数组。如果你调整它们的大小,那么在java中使用数组是没有意义的:它效率不高而且你无法共享该实例。
通常和干净的解决方案是使用List,这是一个可调整大小的集合。 ArrayList,最常用的List实现由数组支持,但效率很高,因为每次调整集合大小时都不会更改数组。
答案 1 :(得分:2)
根据JavaScript MDN specification,这是Array.prototype.splice()
方法的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;
}
以下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) {
}
}
编辑:正如@ denys-séguret所指出的那样,此实现与JavaScript规范不同,因为它不会改变/修改原始数组。相反,此实现返回一个新的数组实例。
编辑:此实现适用于以下maven工件,位于给定的maven repo:
<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>
答案 2 :(得分:0)
在标准Java库中,没有等效的功能。
有java.util.Arrays
类,但没有类似的功能。
答案 3 :(得分:0)
我误解了您的问题并混淆了splice
和slice
。
类java.util.Arrays
提供了一些在使用数组时有用的静态函数。有关其他功能,请参阅官方文档: http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html 。
slice
的Java等价物是:Arrays.copyOfRange(array, from, to)
。
与splice
类似的方法是addAll
(http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#addAll-int-java.util.Collection-)。但是您需要使用java.util.ArrayList
而不是数组,并且无法使用它删除元素。您必须将元素作为另一个集合提供,例如ArrayList
。所以它等同于调用splice(index, 0, element1, element2, ...)
答案 4 :(得分:0)
答案 5 :(得分:0)
Java中的数组具有固定数量的元素。但是你可以将这个元素设为null:
阵列[元素] == NULL;
这与从数组中删除它相同。您还可以使用一个变量来跟踪有多少元素不为空,这样您甚至可以使用array.length之类的东西。无论如何,这就是我所做的。