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>
* 当给定的index大于数组长度则追加
* 将新元素插入到到已有数组中的某个位置<br>
* 添加新元素会生成一个新数组或原有数组<br>
* 如果插入位置为为负数那么生成一个由插入元素顺序加已有数组顺序的新数组
*
* @param <T> 数组元素类型
* @param buffer 已有数组
@ -378,31 +379,33 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @param values 新值
* @return 新数组或原有数组
*/
@SuppressWarnings({"unchecked", "SuspiciousSystemArraycopy"})
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);
}
/**
* 将元素值设置为数组的某个位置,根据元素顺序添加<br>
* 当给定的index大于数组长度则追加
*
* @param <T> 数组元素类型
* @param buffer 已有数组
* @param index 位置大于长度追加否则替换
* @param values 新值
* @return 新数组或原有数组
*/
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]);
if (isEmpty(buffer) || index == 0 && isNotEmpty(values)) {
return values;
}
if (index >= buffer.length || isEmpty(values)) {
return append(buffer, values);
}
int replaceSpace = buffer.length - index - 1;
if (replaceSpace > values.length) {
for (int i = index - 1; i < values.length; i++) {
Array.set(buffer, i + 1, values[i]);
}
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
*/
public static boolean isAllEmpty(Object... args) {
for (Object obj: args) {
for (Object obj : args) {
if (false == ObjectUtil.isEmpty(obj)) {
return false;
}

View File

@ -363,7 +363,7 @@ public class ArrayUtilTest {
}
@Test
public void indexOfSubTest2(){
public void indexOfSubTest2() {
Integer[] a = {0x12, 0x56, 0x34, 0x56, 0x78, 0x9A};
Integer[] b = {0x56, 0x78};
int i = ArrayUtil.indexOfSub(a, b);
@ -401,7 +401,7 @@ public class ArrayUtilTest {
}
@Test
public void lastIndexOfSubTest2(){
public void lastIndexOfSubTest2() {
Integer[] a = {0x12, 0x56, 0x78, 0x56, 0x21, 0x9A};
Integer[] b = {0x56, 0x78};
int i = ArrayUtil.indexOfSub(a, b);
@ -409,17 +409,17 @@ public class ArrayUtilTest {
}
@Test
public void reverseTest(){
int[] a = {1,2,3,4};
public void reverseTest() {
int[] a = {1, 2, 3, 4};
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
public void reverseTest2s(){
Object[] a = {"1",'2',"3",4};
public void reverseTest2s() {
Object[] a = {"1", '2', "3", 4};
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
@ -461,9 +461,63 @@ public class ArrayUtilTest {
}
@Test
public void getTest(){
public void getTest() {
String[] a = {"a", "b", "c"};
final Object o = ArrayUtil.get(a, -1);
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);
}
}