fix ArrayUtil.toString

This commit is contained in:
Looly 2019-09-19 09:43:21 +08:00
parent 470e8f9633
commit 4d46555b2d
4 changed files with 107 additions and 93 deletions

View File

@ -9,6 +9,7 @@
* 【core】 ImgUtil.rotate支持负数issue#543@Github
* 【http】 body方法传null跳过而非报错issue#I12AP2@Gitee
* 【core】 TimeInterval增加intervalPretty方法issue#I12A6T@Gitee
* 【core】 改进ArrayUtil.toString提高性能
### Bug修复
* 【core】 修复DateUtil.offset导致的时区错误问题issue#I1294O@Gitee
* 【core】 修复RuntimeUtil.exec重载导致的问题issue#544@Github

View File

@ -55,7 +55,6 @@ import cn.hutool.core.io.file.LineSeparator;
import cn.hutool.core.io.file.Tailer;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.CharsetUtil;

View File

@ -2,13 +2,7 @@ package cn.hutool.core.util;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.*;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.IterUtil;
@ -160,8 +154,8 @@ public class ArrayUtil {
* @param array 数组
* @return 是否为非空
*/
public static boolean isNotEmpty(final Object array) {
return false == isEmpty((Object) array);
public static boolean isNotEmpty(Object array) {
return false == isEmpty(array);
}
/**
@ -170,7 +164,7 @@ public class ArrayUtil {
* @param array 数组
* @return 是否为非空
*/
public static boolean isNotEmpty(final long... array) {
public static boolean isNotEmpty(long... array) {
return (array != null && array.length != 0);
}
@ -180,7 +174,7 @@ public class ArrayUtil {
* @param array 数组
* @return 是否为非空
*/
public static boolean isNotEmpty(final int... array) {
public static boolean isNotEmpty(int... array) {
return (array != null && array.length != 0);
}
@ -190,7 +184,7 @@ public class ArrayUtil {
* @param array 数组
* @return 是否为非空
*/
public static boolean isNotEmpty(final short... array) {
public static boolean isNotEmpty(short... array) {
return (array != null && array.length != 0);
}
@ -200,7 +194,7 @@ public class ArrayUtil {
* @param array 数组
* @return 是否为非空
*/
public static boolean isNotEmpty(final char... array) {
public static boolean isNotEmpty(char... array) {
return (array != null && array.length != 0);
}
@ -210,7 +204,7 @@ public class ArrayUtil {
* @param array 数组
* @return 是否为非空
*/
public static boolean isNotEmpty(final byte... array) {
public static boolean isNotEmpty(byte... array) {
return (array != null && array.length != 0);
}
@ -220,7 +214,7 @@ public class ArrayUtil {
* @param array 数组
* @return 是否为非空
*/
public static boolean isNotEmpty(final double... array) {
public static boolean isNotEmpty(double... array) {
return (array != null && array.length != 0);
}
@ -230,7 +224,7 @@ public class ArrayUtil {
* @param array 数组
* @return 是否为非空
*/
public static boolean isNotEmpty(final float... array) {
public static boolean isNotEmpty(float... array) {
return (array != null && array.length != 0);
}
@ -240,7 +234,7 @@ public class ArrayUtil {
* @param array 数组
* @return 是否为非空
*/
public static boolean isNotEmpty(final boolean... array) {
public static boolean isNotEmpty(boolean... array) {
return (array != null && array.length != 0);
}
@ -733,7 +727,7 @@ public class ArrayUtil {
* @return 过滤后的数组
*/
public static <T> T[] filter(T[] array, Editor<T> editor) {
ArrayList<T> list = new ArrayList<T>(array.length);
ArrayList<T> list = new ArrayList<>(array.length);
T modified;
for (T t : array) {
modified = editor.edit(t);
@ -763,7 +757,7 @@ public class ArrayUtil {
return array;
}
final ArrayList<T> list = new ArrayList<T>(array.length);
final ArrayList<T> list = new ArrayList<>(array.length);
for (T t : array) {
if (filter.accept(t)) {
list.add(t);
@ -2228,33 +2222,32 @@ public class ArrayUtil {
if (null == obj) {
return null;
}
if (ArrayUtil.isArray(obj)) {
if(obj instanceof long[]){
return Arrays.toString((long[]) obj);
} else if(obj instanceof int[]){
return Arrays.toString((int[]) obj);
} else if(obj instanceof short[]){
return Arrays.toString((short[]) obj);
} else if(obj instanceof char[]){
return Arrays.toString((char[]) obj);
} else if(obj instanceof byte[]){
return Arrays.toString((byte[]) obj);
} else if(obj instanceof boolean[]){
return Arrays.toString((boolean[]) obj);
} else if(obj instanceof float[]){
return Arrays.toString((float[]) obj);
} else if(obj instanceof double[]){
return Arrays.toString((double[]) obj);
} else if (ArrayUtil.isArray(obj)) {
// 对象数组
try {
return Arrays.deepToString((Object[]) obj);
} catch (Exception e) {
final String className = obj.getClass().getComponentType().getName();
switch (className) {
case "long":
return Arrays.toString((long[]) obj);
case "int":
return Arrays.toString((int[]) obj);
case "short":
return Arrays.toString((short[]) obj);
case "char":
return Arrays.toString((char[]) obj);
case "byte":
return Arrays.toString((byte[]) obj);
case "boolean":
return Arrays.toString((boolean[]) obj);
case "float":
return Arrays.toString((float[]) obj);
case "double":
return Arrays.toString((double[]) obj);
default:
throw new UtilException(e);
}
} catch (Exception ignore) {
//ignore
}
}
return obj.toString();
}
@ -2775,7 +2768,7 @@ public class ArrayUtil {
*/
public static Object remove(Object array, int index) throws IllegalArgumentException {
if (null == array) {
return array;
return null;
}
int length = length(array);
if (index < 0 || index >= length) {
@ -2927,7 +2920,7 @@ public class ArrayUtil {
*
* @param <T> 数组元素类型
* @param array 数组会变更
* @param startIndexInclusive 其实位置包含
* @param startIndexInclusive 开始位置包含
* @param endIndexExclusive 结束位置不包含
* @return 变更后的原数组
* @since 3.0.9
@ -2936,7 +2929,7 @@ public class ArrayUtil {
if (isEmpty(array)) {
return array;
}
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
int i = Math.max(startIndexInclusive, 0);
int j = Math.min(array.length, endIndexExclusive) - 1;
T tmp;
while (j > i) {
@ -2974,7 +2967,7 @@ public class ArrayUtil {
if (isEmpty(array)) {
return array;
}
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
int i = Math.max(startIndexInclusive, 0);
int j = Math.min(array.length, endIndexExclusive) - 1;
long tmp;
while (j > i) {
@ -3011,7 +3004,7 @@ public class ArrayUtil {
if (isEmpty(array)) {
return array;
}
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
int i = Math.max(startIndexInclusive, 0);
int j = Math.min(array.length, endIndexExclusive) - 1;
int tmp;
while (j > i) {
@ -3048,7 +3041,7 @@ public class ArrayUtil {
if (isEmpty(array)) {
return array;
}
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
int i = Math.max(startIndexInclusive, 0);
int j = Math.min(array.length, endIndexExclusive) - 1;
short tmp;
while (j > i) {
@ -3085,7 +3078,7 @@ public class ArrayUtil {
if (isEmpty(array)) {
return array;
}
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
int i = Math.max(startIndexInclusive, 0);
int j = Math.min(array.length, endIndexExclusive) - 1;
char tmp;
while (j > i) {
@ -3122,7 +3115,7 @@ public class ArrayUtil {
if (isEmpty(array)) {
return array;
}
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
int i = Math.max(startIndexInclusive, 0);
int j = Math.min(array.length, endIndexExclusive) - 1;
byte tmp;
while (j > i) {
@ -3159,7 +3152,7 @@ public class ArrayUtil {
if (isEmpty(array)) {
return array;
}
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
int i = Math.max(startIndexInclusive, 0);
int j = Math.min(array.length, endIndexExclusive) - 1;
double tmp;
while (j > i) {
@ -3196,7 +3189,7 @@ public class ArrayUtil {
if (isEmpty(array)) {
return array;
}
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
int i = Math.max(startIndexInclusive, 0);
int j = Math.min(array.length, endIndexExclusive) - 1;
float tmp;
while (j > i) {
@ -3233,7 +3226,7 @@ public class ArrayUtil {
if (isEmpty(array)) {
return array;
}
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
int i = Math.max(startIndexInclusive, 0);
int j = Math.min(array.length, endIndexExclusive) - 1;
boolean tmp;
while (j > i) {
@ -3844,9 +3837,7 @@ public class ArrayUtil {
}
final Set<T> set = new LinkedHashSet<>(array.length, 1);
for (T t : array) {
set.add(t);
}
Collections.addAll(set, array);
return toArray(set, (Class<T>)getComponentType(array));
}
}

View File

@ -2,6 +2,7 @@ package cn.hutool.core.util;
import java.util.Map;
import cn.hutool.core.lang.Console;
import org.junit.Assert;
import org.junit.Test;
@ -10,9 +11,8 @@ import cn.hutool.core.lang.Filter;
/**
* {@link ArrayUtil} 数组工具单元测试
*
* @author Looly
*
* @author Looly
*/
public class ArrayUtilTest {
@ -29,7 +29,7 @@ public class ArrayUtilTest {
@Test
public void isNotEmptyTest() {
int[] a = { 1, 2 };
int[] a = {1, 2};
Assert.assertTrue(ArrayUtil.isNotEmpty(a));
}
@ -38,98 +38,98 @@ public class ArrayUtilTest {
String[] newArray = ArrayUtil.newArray(String.class, 3);
Assert.assertEquals(3, newArray.length);
}
@Test
public void cloneTest() {
Integer[] b = { 1, 2, 3 };
Integer[] b = {1, 2, 3};
Integer[] cloneB = ArrayUtil.clone(b);
Assert.assertArrayEquals(b, cloneB);
int[] a = { 1, 2, 3 };
int[] a = {1, 2, 3};
int[] clone = ArrayUtil.clone(a);
Assert.assertArrayEquals(a, clone);
}
@Test
public void filterTest() {
Integer[] a = { 1, 2, 3, 4, 5, 6 };
Integer[] a = {1, 2, 3, 4, 5, 6};
Integer[] filter = ArrayUtil.filter(a, new Editor<Integer>() {
@Override
public Integer edit(Integer t) {
return (t % 2 == 0) ? t : null;
}
});
Assert.assertArrayEquals(filter, new Integer[] { 2, 4, 6 });
Assert.assertArrayEquals(filter, new Integer[]{2, 4, 6});
}
@Test
public void filterTestForFilter() {
Integer[] a = { 1, 2, 3, 4, 5, 6 };
Integer[] a = {1, 2, 3, 4, 5, 6};
Integer[] filter = ArrayUtil.filter(a, new Filter<Integer>() {
@Override
public boolean accept(Integer t) {
return t % 2 == 0;
}
});
Assert.assertArrayEquals(filter, new Integer[] { 2, 4, 6 });
Assert.assertArrayEquals(filter, new Integer[]{2, 4, 6});
}
@Test
public void filterTestForEditor() {
Integer[] a = { 1, 2, 3, 4, 5, 6 };
Integer[] a = {1, 2, 3, 4, 5, 6};
Integer[] filter = ArrayUtil.filter(a, new Editor<Integer>() {
@Override
public Integer edit(Integer t) {
return (t % 2 == 0) ? t * 10 : t;
}
});
Assert.assertArrayEquals(filter, new Integer[] { 1, 20, 3, 40, 5, 60 });
Assert.assertArrayEquals(filter, new Integer[]{1, 20, 3, 40, 5, 60});
}
@Test
public void indexOfTest() {
Integer[] a = { 1, 2, 3, 4, 5, 6 };
Integer[] a = {1, 2, 3, 4, 5, 6};
int index = ArrayUtil.indexOf(a, 3);
Assert.assertEquals(2, index);
long[] b = { 1, 2, 3, 4, 5, 6 };
long[] b = {1, 2, 3, 4, 5, 6};
int index2 = ArrayUtil.indexOf(b, 3);
Assert.assertEquals(2, index2);
}
@Test
public void lastIndexOfTest() {
Integer[] a = { 1, 2, 3, 4, 3, 6 };
Integer[] a = {1, 2, 3, 4, 3, 6};
int index = ArrayUtil.lastIndexOf(a, 3);
Assert.assertEquals(4, index);
long[] b = { 1, 2, 3, 4, 3, 6 };
long[] b = {1, 2, 3, 4, 3, 6};
int index2 = ArrayUtil.lastIndexOf(b, 3);
Assert.assertEquals(4, index2);
}
@Test
public void containsTest() {
Integer[] a = { 1, 2, 3, 4, 3, 6 };
Integer[] a = {1, 2, 3, 4, 3, 6};
boolean contains = ArrayUtil.contains(a, 3);
Assert.assertTrue(contains);
long[] b = { 1, 2, 3, 4, 3, 6 };
long[] b = {1, 2, 3, 4, 3, 6};
boolean contains2 = ArrayUtil.contains(b, 3);
Assert.assertTrue(contains2);
}
@Test
public void mapTest() {
String[] keys = { "a", "b", "c" };
Integer[] values = { 1, 2, 3 };
String[] keys = {"a", "b", "c"};
Integer[] values = {1, 2, 3};
Map<String, Integer> map = ArrayUtil.zip(keys, values, true);
Assert.assertEquals(map.toString(), "{a=1, b=2, c=3}");
}
@Test
public void castTest() {
Object[] values = { "1", "2", "3" };
Object[] values = {"1", "2", "3"};
String[] cast = (String[]) ArrayUtil.cast(String.class, values);
Assert.assertEquals(values[0], cast[0]);
Assert.assertEquals(values[1], cast[1]);
@ -177,59 +177,82 @@ public class ArrayUtilTest {
@Test
public void appendTest() {
String[] a = { "1", "2", "3", "4" };
String[] b = { "a", "b", "c" };
String[] a = {"1", "2", "3", "4"};
String[] b = {"a", "b", "c"};
String[] result = ArrayUtil.append(a, b);
Assert.assertArrayEquals(new String[] { "1", "2", "3", "4", "a", "b", "c" }, result);
Assert.assertArrayEquals(new String[]{"1", "2", "3", "4", "a", "b", "c"}, result);
}
@Test
public void insertTest() {
String[] a = { "1", "2", "3", "4" };
String[] b = { "a", "b", "c" };
String[] a = {"1", "2", "3", "4"};
String[] b = {"a", "b", "c"};
// -1位置插入相当于在3位置插入
String[] result = ArrayUtil.insert(a, -1, b);
Assert.assertArrayEquals(new String[] { "1", "2", "3", "a", "b", "c", "4" }, result);
Assert.assertArrayEquals(new String[]{"1", "2", "3", "a", "b", "c", "4"}, result);
// 在第0个位置插入既在数组前追加
result = ArrayUtil.insert(a, 0, b);
Assert.assertArrayEquals(new String[] { "a", "b", "c", "1", "2", "3", "4" }, result);
Assert.assertArrayEquals(new String[]{"a", "b", "c", "1", "2", "3", "4"}, result);
// 在第2个位置插入"3"之前
result = ArrayUtil.insert(a, 2, b);
Assert.assertArrayEquals(new String[] { "1", "2", "a", "b", "c", "3", "4" }, result);
Assert.assertArrayEquals(new String[]{"1", "2", "a", "b", "c", "3", "4"}, result);
// 在第4个位置插入"4"之后相当于追加
result = ArrayUtil.insert(a, 4, b);
Assert.assertArrayEquals(new String[] { "1", "2", "3", "4", "a", "b", "c" }, result);
Assert.assertArrayEquals(new String[]{"1", "2", "3", "4", "a", "b", "c"}, result);
// 在第5个位置插入由于数组长度为4因此补null
result = ArrayUtil.insert(a, 5, b);
Assert.assertArrayEquals(new String[] { "1", "2", "3", "4", null, "a", "b", "c" }, result);
Assert.assertArrayEquals(new String[]{"1", "2", "3", "4", null, "a", "b", "c"}, result);
}
@Test
public void joinTest() {
String[] array = {"aa", "bb", "cc", "dd"};
String join = ArrayUtil.join(array, ",", "[", "]");
Assert.assertEquals("[aa],[bb],[cc],[dd]", join);
}
@Test
public void getArrayTypeTest() {
Class<?> arrayType = ArrayUtil.getArrayType(int.class);
Assert.assertEquals(int[].class, arrayType);
arrayType = ArrayUtil.getArrayType(String.class);
Assert.assertEquals(String[].class, arrayType);
}
@Test
public void distinctTest() {
String[] array = {"aa", "bb", "cc", "dd", "bb", "dd"};
String[] distinct = ArrayUtil.distinct(array);
Assert.assertArrayEquals(new String[] {"aa", "bb", "cc", "dd"}, distinct);
Assert.assertArrayEquals(new String[]{"aa", "bb", "cc", "dd"}, distinct);
}
@Test
public void toStingTest() {
int[] a = {1, 3, 56, 6, 7};
Assert.assertEquals("[1, 3, 56, 6, 7]", ArrayUtil.toString(a));
long[] b = {1, 3, 56, 6, 7};
Assert.assertEquals("[1, 3, 56, 6, 7]", ArrayUtil.toString(b));
short[] c = {1, 3, 56, 6, 7};
Assert.assertEquals("[1, 3, 56, 6, 7]", ArrayUtil.toString(c));
double[] d = {1, 3, 56, 6, 7};
Assert.assertEquals("[1.0, 3.0, 56.0, 6.0, 7.0]", ArrayUtil.toString(d));
byte[] e = {1, 3, 56, 6, 7};
Assert.assertEquals("[1, 3, 56, 6, 7]", ArrayUtil.toString(e));
boolean[] f = {true, false, true, true, true};
Assert.assertEquals("[true, false, true, true, true]", ArrayUtil.toString(f));
float[] g = {1, 3, 56, 6, 7};
Assert.assertEquals("[1.0, 3.0, 56.0, 6.0, 7.0]", ArrayUtil.toString(g));
char[] h = {'a', 'b', '你', '好', '1'};
Assert.assertEquals("[a, b, 你, 好, 1]", ArrayUtil.toString(h));
String[] array = {"aa", "bb", "cc", "dd", "bb", "dd"};
Assert.assertEquals("[aa, bb, cc, dd, bb, dd]", ArrayUtil.toString(array));
}
}