This commit is contained in:
Looly 2024-01-24 17:36:30 +08:00
parent 79ef7de544
commit f368a0a0d2
2 changed files with 12 additions and 5 deletions

View File

@ -990,11 +990,9 @@ public class ArrayUtil extends PrimitiveArrayUtil {
final Class<?> componentType = obj.getClass().getComponentType(); final Class<?> componentType = obj.getClass().getComponentType();
// 原始类型 // 原始类型
if (componentType.isPrimitive()) { if (componentType.isPrimitive()) {
int length = Array.getLength(obj); final int length = Array.getLength(obj);
result = Array.newInstance(componentType, length); result = Array.newInstance(componentType, length);
while (length-- > 0) { copy(obj, result, length);
Array.set(result, length, Array.get(obj, length));
}
} else { } else {
result = ((Object[]) obj).clone(); result = ((Object[]) obj).clone();
} }

View File

@ -90,6 +90,9 @@ public class ArrayUtilTest {
final int[] a = {1, 2, 3}; final int[] a = {1, 2, 3};
final int[] clone = ArrayUtil.clone(a); final int[] clone = ArrayUtil.clone(a);
Assertions.assertArrayEquals(a, clone); Assertions.assertArrayEquals(a, clone);
final int[] clone1 = a.clone();
Assertions.assertArrayEquals(a, clone1);
} }
@Test @Test
@ -606,10 +609,16 @@ public class ArrayUtilTest {
void setOrPaddingTest2(){ void setOrPaddingTest2(){
final String[] arr = new String[0]; final String[] arr = new String[0];
final String[] newArr = ArrayUtil.setOrPadding(arr, 2, "Good"); final String[] newArr = ArrayUtil.setOrPadding(arr, 2, "Good");
Console.log(newArr);
Assertions.assertArrayEquals(new String[]{null, null, "Good"}, newArr); Assertions.assertArrayEquals(new String[]{null, null, "Good"}, newArr);
} }
@Test
void setOrPaddingTest3(){
final String[] arr = new String[0];
final String[] newArr = ArrayUtil.setOrPadding(arr, 2, "Good", "pad");
Assertions.assertArrayEquals(new String[]{"pad", "pad", "Good"}, newArr);
}
@Test @Test
public void getAnyTest() { public void getAnyTest() {
final String[] a = {"a", "b", "c", "d", "e"}; final String[] a = {"a", "b", "c", "d", "e"};