Husky 2022-03-13 23:07:41 +08:00
parent 78f886075e
commit c62d4d382a
2 changed files with 93 additions and 36 deletions

View File

@ -369,8 +369,9 @@ public class ArrayUtil extends PrimitiveArrayUtil {
} }
/** /**
* 将元素值设置为数组的某个位置,根据元素顺序添加<br> * 将新元素插入到到已有数组中的某个位置<br>
* 当给定的index大于数组长度则追加 * 添加新元素会生成一个新数组或原有数组<br>
* 如果插入位置为为负数那么生成一个由插入元素顺序加已有数组顺序的新数组
* *
* @param <T> 数组元素类型 * @param <T> 数组元素类型
* @param buffer 已有数组 * @param buffer 已有数组
@ -378,31 +379,33 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @param values 新值 * @param values 新值
* @return 新数组或原有数组 * @return 新数组或原有数组
*/ */
@SuppressWarnings({"unchecked", "SuspiciousSystemArraycopy"})
public static <T> T[] replace(T[] buffer, int index, T... values) { public static <T> T[] replace(T[] buffer, int index, T... values) {
return index == 0 ? values : replaceBy(buffer, index, values); if (index < 0) {
} return insert(buffer, 0, values);
}
/** if (isEmpty(buffer) || index == 0 && isNotEmpty(values)) {
* 将元素值设置为数组的某个位置,根据元素顺序添加<br> return values;
* 当给定的index大于数组长度则追加 }
* if (index >= buffer.length || isEmpty(values)) {
* @param <T> 数组元素类型 return append(buffer, values);
* @param buffer 已有数组 }
* @param index 位置大于长度追加否则替换 int replaceSpace = buffer.length - index - 1;
* @param values 新值 if (replaceSpace > values.length) {
* @return 新数组或原有数组 for (int i = index - 1; i < values.length; i++) {
*/ Array.set(buffer, i + 1, values[i]);
public static <T> T[] replaceBy(T[] buffer, int index, T... values) {
if (index < buffer.length && buffer.length - index - 1 >= values.length) {
for (int i = index; i < values.length; i++) {
Array.set(buffer, index, values[i]);
} }
return buffer; return buffer;
} else {
final T[] result = (T[]) Array.newInstance(buffer.getClass().getComponentType(), buffer.length - index - 1);
System.arraycopy(buffer, 0, result, 0, buffer.length - index - 1);
return append(result, values);
} }
int newArrayLength = index + values.length;
final T[] result = (T[]) Array.newInstance(buffer.getClass().getComponentType(), newArrayLength);
System.arraycopy(buffer, 0, result, 0, index);
int valueIndex = 0;
for (int i = index; i < newArrayLength; i++) {
Array.set(result, i, values[valueIndex]);
valueIndex = valueIndex + 1;
}
return result;
} }
/** /**
@ -1577,7 +1580,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @since 4.5.18 * @since 4.5.18
*/ */
public static boolean isAllEmpty(Object... args) { public static boolean isAllEmpty(Object... args) {
for (Object obj: args) { for (Object obj : args) {
if (false == ObjectUtil.isEmpty(obj)) { if (false == ObjectUtil.isEmpty(obj)) {
return false; return false;
} }
@ -1795,10 +1798,10 @@ public class ArrayUtil extends PrimitiveArrayUtil {
/** /**
* 查找最后一个子数组的开始位置 * 查找最后一个子数组的开始位置
* *
* @param array 数组 * @param array 数组
* @param endInclude 查找结束的位置包含 * @param endInclude 查找结束的位置包含
* @param subArray 子数组 * @param subArray 子数组
* @param <T> 数组元素类型 * @param <T> 数组元素类型
* @return 最后一个子数组的开始位置即子数字第一个元素在数组中的位置 * @return 最后一个子数组的开始位置即子数字第一个元素在数组中的位置
* @since 5.4.8 * @since 5.4.8
*/ */

View File

@ -363,7 +363,7 @@ public class ArrayUtilTest {
} }
@Test @Test
public void indexOfSubTest2(){ public void indexOfSubTest2() {
Integer[] a = {0x12, 0x56, 0x34, 0x56, 0x78, 0x9A}; Integer[] a = {0x12, 0x56, 0x34, 0x56, 0x78, 0x9A};
Integer[] b = {0x56, 0x78}; Integer[] b = {0x56, 0x78};
int i = ArrayUtil.indexOfSub(a, b); int i = ArrayUtil.indexOfSub(a, b);
@ -401,7 +401,7 @@ public class ArrayUtilTest {
} }
@Test @Test
public void lastIndexOfSubTest2(){ public void lastIndexOfSubTest2() {
Integer[] a = {0x12, 0x56, 0x78, 0x56, 0x21, 0x9A}; Integer[] a = {0x12, 0x56, 0x78, 0x56, 0x21, 0x9A};
Integer[] b = {0x56, 0x78}; Integer[] b = {0x56, 0x78};
int i = ArrayUtil.indexOfSub(a, b); int i = ArrayUtil.indexOfSub(a, b);
@ -409,17 +409,17 @@ public class ArrayUtilTest {
} }
@Test @Test
public void reverseTest(){ public void reverseTest() {
int[] a = {1,2,3,4}; int[] a = {1, 2, 3, 4};
final int[] reverse = ArrayUtil.reverse(a); final int[] reverse = ArrayUtil.reverse(a);
Assert.assertArrayEquals(new int[]{4,3,2,1}, reverse); Assert.assertArrayEquals(new int[]{4, 3, 2, 1}, reverse);
} }
@Test @Test
public void reverseTest2s(){ public void reverseTest2s() {
Object[] a = {"1",'2',"3",4}; Object[] a = {"1", '2', "3", 4};
final Object[] reverse = ArrayUtil.reverse(a); final Object[] reverse = ArrayUtil.reverse(a);
Assert.assertArrayEquals(new Object[]{4,"3",'2',"1"}, reverse); Assert.assertArrayEquals(new Object[]{4, "3", '2', "1"}, reverse);
} }
@Test @Test
@ -461,9 +461,63 @@ public class ArrayUtilTest {
} }
@Test @Test
public void getTest(){ public void getTest() {
String[] a = {"a", "b", "c"}; String[] a = {"a", "b", "c"};
final Object o = ArrayUtil.get(a, -1); final Object o = ArrayUtil.get(a, -1);
Assert.assertEquals("c", o); Assert.assertEquals("c", o);
} }
@Test
public void replaceTest() {
String[] a = {"1", "2", "3", "4"};
String[] b = {"a", "b", "c"};
// 在小于0的位置-1位置插入返回b+a
String[] result = ArrayUtil.replace(a, -1, b);
Assert.assertArrayEquals(new String[]{"a", "b", "c", "1", "2", "3", "4"}, result);
// 在第0个位置插入即覆盖a直接返回b
result = ArrayUtil.replace(a, 0, b);
Assert.assertArrayEquals(new String[]{"a", "b", "c"}, result);
// 在第1个位置插入"2"之前
result = ArrayUtil.replace(a, 1, b);
Assert.assertArrayEquals(new String[]{"1", "a", "b", "c"}, result);
//上一步测试修改了原数组结构
String[] c = {"1", "2", "3", "4"};
String[] d = {"a", "b", "c"};
// 在第2个位置插入"3"之后
result = ArrayUtil.replace(c, 2, d);
Assert.assertArrayEquals(new String[]{"1", "2", "a", "b", "c"}, result);
// 在第3个位置插入"4"之后
result = ArrayUtil.replace(c, 3, d);
Assert.assertArrayEquals(new String[]{"1", "2", "3", "a", "b", "c"}, result);
// 在第4个位置插入数组长度为4在索引4出替换即两个数组相加
result = ArrayUtil.replace(c, 4, d);
Assert.assertArrayEquals(new String[]{"1", "2", "3", "4", "a", "b", "c"}, result);
// 在大于3个位置插入数组长度为4即两个数组相加
result = ArrayUtil.replace(c, 5, d);
Assert.assertArrayEquals(new String[]{"1", "2", "3", "4", "a", "b", "c"}, result);
//上一步测试修改了原数组结构
String[] e = null;
String[] f = {"a", "b", "c"};
// e为null 返回 f
result = ArrayUtil.replace(e, -1, f);
Assert.assertArrayEquals(new String[]{"a", "b", "c"}, result);
//上一步测试修改了原数组结构
String[] g = {"a", "b", "c"};
String[] h = null;
// h为null 返回 g
result = ArrayUtil.replace(g, 0, h);
Assert.assertArrayEquals(new String[]{"a", "b", "c"}, result);
}
} }