someArray.splice(a,b,...)
JavaScript中的方法在数组中添加项或从数组中删除项。用Java语言实现这种方法的最佳和简单解决方案是什么?假设我们有String[]
数组。
这是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;
}
以下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工件可以使用此实现:
<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>
问题内容: 在C#中,您可以匿名定义委托(即使它们不过是语法糖)。例如,我可以这样做: 是否可以在Java中传递这样的代码?我正在使用处理框架,该框架具有Java的较旧版本(它没有泛型)。 问题答案: Java 8之前的版本: 最接近委托的Java是单方法接口。您可以使用匿名内部类。 Java 8及更高版本: Java 8 在该语言中添加了lambda表达式。
我是PHP新手 默认情况下,rails框架中提供了以下脏方法,这里的是模型对象,表示表中的行。 我对感兴趣
问题内容: 编写自定义类时,通过和运算符允许等效性通常很重要。在Python中,这可以通过分别实现和特殊方法来实现。我发现执行此操作的最简单方法是以下方法: 你知道这样做更优雅的方法吗?你知道使用上述 比较方法有什么特别的缺点吗? 注意:需要澄清的一点-当和未定义时,你会发现以下行为: 也就是说,评估为False因为它确实运行了,所以对身份进行了测试(即“ a与b?是同一对象”)。 当和定义,你会
我在Spring后端项目中有方法A,在执行不同的方法B后应该调用它。返回方法B的类型是void,但在成功执行方法B后,我需要将一个输入参数从方法B传递到方法A。
本文向大家介绍原生JS中slice()方法和splice()区别,包括了原生JS中slice()方法和splice()区别的使用技巧和注意事项,需要的朋友参考一下 slice()方法和splice()方法都是原生js中对数组操作的方法。那么他两种有什么区别呢?今天通过本文教程给大家简单介绍下。 slice(),返回一个新的数组,该方法可从已有的数组中返回选定的元素。例如:arrObject(sta
问题内容: 我刚刚发现了一个非常有趣的Java技巧: 因此,您可以根据需要为该方法提供尽可能多的整数。 现在,如果我有一个类似的(重载)方法,例如: 当我执行以下行时,运行哪种方法: 好吧,我可以通过使用不同的方法指令进行测试来轻松地发现这一点,但是当我考虑“重载”方法中的“规则”时,我必须确保每个重载的方法都必须相同,以便编译器知道确切地使用哪个。 我认为,上面的代码不起作用,因为应该混淆编译器