mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix ArrayUtil.toString
This commit is contained in:
@@ -9,6 +9,7 @@
|
|||||||
* 【core】 ImgUtil.rotate支持负数(issue#543@Github)
|
* 【core】 ImgUtil.rotate支持负数(issue#543@Github)
|
||||||
* 【http】 body方法传null跳过而非报错(issue#I12AP2@Gitee)
|
* 【http】 body方法传null跳过而非报错(issue#I12AP2@Gitee)
|
||||||
* 【core】 TimeInterval增加intervalPretty方法(issue#I12A6T@Gitee)
|
* 【core】 TimeInterval增加intervalPretty方法(issue#I12A6T@Gitee)
|
||||||
|
* 【core】 改进ArrayUtil.toString,提高性能
|
||||||
### Bug修复
|
### Bug修复
|
||||||
* 【core】 修复DateUtil.offset导致的时区错误问题(issue#I1294O@Gitee)
|
* 【core】 修复DateUtil.offset导致的时区错误问题(issue#I1294O@Gitee)
|
||||||
* 【core】 修复RuntimeUtil.exec重载导致的问题(issue#544@Github)
|
* 【core】 修复RuntimeUtil.exec重载导致的问题(issue#544@Github)
|
||||||
|
@@ -55,7 +55,6 @@ import cn.hutool.core.io.file.LineSeparator;
|
|||||||
import cn.hutool.core.io.file.Tailer;
|
import cn.hutool.core.io.file.Tailer;
|
||||||
import cn.hutool.core.io.resource.ResourceUtil;
|
import cn.hutool.core.io.resource.ResourceUtil;
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.lang.Console;
|
|
||||||
import cn.hutool.core.util.ArrayUtil;
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
import cn.hutool.core.util.CharUtil;
|
import cn.hutool.core.util.CharUtil;
|
||||||
import cn.hutool.core.util.CharsetUtil;
|
import cn.hutool.core.util.CharsetUtil;
|
||||||
|
@@ -2,13 +2,7 @@ package cn.hutool.core.util;
|
|||||||
|
|
||||||
import java.lang.reflect.Array;
|
import java.lang.reflect.Array;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
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 cn.hutool.core.collection.CollectionUtil;
|
import cn.hutool.core.collection.CollectionUtil;
|
||||||
import cn.hutool.core.collection.IterUtil;
|
import cn.hutool.core.collection.IterUtil;
|
||||||
@@ -160,8 +154,8 @@ public class ArrayUtil {
|
|||||||
* @param array 数组
|
* @param array 数组
|
||||||
* @return 是否为非空
|
* @return 是否为非空
|
||||||
*/
|
*/
|
||||||
public static boolean isNotEmpty(final Object array) {
|
public static boolean isNotEmpty(Object array) {
|
||||||
return false == isEmpty((Object) array);
|
return false == isEmpty(array);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -170,7 +164,7 @@ public class ArrayUtil {
|
|||||||
* @param array 数组
|
* @param array 数组
|
||||||
* @return 是否为非空
|
* @return 是否为非空
|
||||||
*/
|
*/
|
||||||
public static boolean isNotEmpty(final long... array) {
|
public static boolean isNotEmpty(long... array) {
|
||||||
return (array != null && array.length != 0);
|
return (array != null && array.length != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -180,7 +174,7 @@ public class ArrayUtil {
|
|||||||
* @param array 数组
|
* @param array 数组
|
||||||
* @return 是否为非空
|
* @return 是否为非空
|
||||||
*/
|
*/
|
||||||
public static boolean isNotEmpty(final int... array) {
|
public static boolean isNotEmpty(int... array) {
|
||||||
return (array != null && array.length != 0);
|
return (array != null && array.length != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -190,7 +184,7 @@ public class ArrayUtil {
|
|||||||
* @param array 数组
|
* @param array 数组
|
||||||
* @return 是否为非空
|
* @return 是否为非空
|
||||||
*/
|
*/
|
||||||
public static boolean isNotEmpty(final short... array) {
|
public static boolean isNotEmpty(short... array) {
|
||||||
return (array != null && array.length != 0);
|
return (array != null && array.length != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,7 +194,7 @@ public class ArrayUtil {
|
|||||||
* @param array 数组
|
* @param array 数组
|
||||||
* @return 是否为非空
|
* @return 是否为非空
|
||||||
*/
|
*/
|
||||||
public static boolean isNotEmpty(final char... array) {
|
public static boolean isNotEmpty(char... array) {
|
||||||
return (array != null && array.length != 0);
|
return (array != null && array.length != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,7 +204,7 @@ public class ArrayUtil {
|
|||||||
* @param array 数组
|
* @param array 数组
|
||||||
* @return 是否为非空
|
* @return 是否为非空
|
||||||
*/
|
*/
|
||||||
public static boolean isNotEmpty(final byte... array) {
|
public static boolean isNotEmpty(byte... array) {
|
||||||
return (array != null && array.length != 0);
|
return (array != null && array.length != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -220,7 +214,7 @@ public class ArrayUtil {
|
|||||||
* @param array 数组
|
* @param array 数组
|
||||||
* @return 是否为非空
|
* @return 是否为非空
|
||||||
*/
|
*/
|
||||||
public static boolean isNotEmpty(final double... array) {
|
public static boolean isNotEmpty(double... array) {
|
||||||
return (array != null && array.length != 0);
|
return (array != null && array.length != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -230,7 +224,7 @@ public class ArrayUtil {
|
|||||||
* @param array 数组
|
* @param array 数组
|
||||||
* @return 是否为非空
|
* @return 是否为非空
|
||||||
*/
|
*/
|
||||||
public static boolean isNotEmpty(final float... array) {
|
public static boolean isNotEmpty(float... array) {
|
||||||
return (array != null && array.length != 0);
|
return (array != null && array.length != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -240,7 +234,7 @@ public class ArrayUtil {
|
|||||||
* @param array 数组
|
* @param array 数组
|
||||||
* @return 是否为非空
|
* @return 是否为非空
|
||||||
*/
|
*/
|
||||||
public static boolean isNotEmpty(final boolean... array) {
|
public static boolean isNotEmpty(boolean... array) {
|
||||||
return (array != null && array.length != 0);
|
return (array != null && array.length != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -733,7 +727,7 @@ public class ArrayUtil {
|
|||||||
* @return 过滤后的数组
|
* @return 过滤后的数组
|
||||||
*/
|
*/
|
||||||
public static <T> T[] filter(T[] array, Editor<T> editor) {
|
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;
|
T modified;
|
||||||
for (T t : array) {
|
for (T t : array) {
|
||||||
modified = editor.edit(t);
|
modified = editor.edit(t);
|
||||||
@@ -763,7 +757,7 @@ public class ArrayUtil {
|
|||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
final ArrayList<T> list = new ArrayList<T>(array.length);
|
final ArrayList<T> list = new ArrayList<>(array.length);
|
||||||
for (T t : array) {
|
for (T t : array) {
|
||||||
if (filter.accept(t)) {
|
if (filter.accept(t)) {
|
||||||
list.add(t);
|
list.add(t);
|
||||||
@@ -2228,33 +2222,32 @@ public class ArrayUtil {
|
|||||||
if (null == obj) {
|
if (null == obj) {
|
||||||
return null;
|
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 {
|
try {
|
||||||
return Arrays.deepToString((Object[]) obj);
|
return Arrays.deepToString((Object[]) obj);
|
||||||
} catch (Exception e) {
|
} catch (Exception ignore) {
|
||||||
final String className = obj.getClass().getComponentType().getName();
|
//ignore
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return obj.toString();
|
return obj.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2775,7 +2768,7 @@ public class ArrayUtil {
|
|||||||
*/
|
*/
|
||||||
public static Object remove(Object array, int index) throws IllegalArgumentException {
|
public static Object remove(Object array, int index) throws IllegalArgumentException {
|
||||||
if (null == array) {
|
if (null == array) {
|
||||||
return array;
|
return null;
|
||||||
}
|
}
|
||||||
int length = length(array);
|
int length = length(array);
|
||||||
if (index < 0 || index >= length) {
|
if (index < 0 || index >= length) {
|
||||||
@@ -2927,7 +2920,7 @@ public class ArrayUtil {
|
|||||||
*
|
*
|
||||||
* @param <T> 数组元素类型
|
* @param <T> 数组元素类型
|
||||||
* @param array 数组,会变更
|
* @param array 数组,会变更
|
||||||
* @param startIndexInclusive 其实位置(包含)
|
* @param startIndexInclusive 开始位置(包含)
|
||||||
* @param endIndexExclusive 结束位置(不包含)
|
* @param endIndexExclusive 结束位置(不包含)
|
||||||
* @return 变更后的原数组
|
* @return 变更后的原数组
|
||||||
* @since 3.0.9
|
* @since 3.0.9
|
||||||
@@ -2936,7 +2929,7 @@ public class ArrayUtil {
|
|||||||
if (isEmpty(array)) {
|
if (isEmpty(array)) {
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
|
int i = Math.max(startIndexInclusive, 0);
|
||||||
int j = Math.min(array.length, endIndexExclusive) - 1;
|
int j = Math.min(array.length, endIndexExclusive) - 1;
|
||||||
T tmp;
|
T tmp;
|
||||||
while (j > i) {
|
while (j > i) {
|
||||||
@@ -2974,7 +2967,7 @@ public class ArrayUtil {
|
|||||||
if (isEmpty(array)) {
|
if (isEmpty(array)) {
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
|
int i = Math.max(startIndexInclusive, 0);
|
||||||
int j = Math.min(array.length, endIndexExclusive) - 1;
|
int j = Math.min(array.length, endIndexExclusive) - 1;
|
||||||
long tmp;
|
long tmp;
|
||||||
while (j > i) {
|
while (j > i) {
|
||||||
@@ -3011,7 +3004,7 @@ public class ArrayUtil {
|
|||||||
if (isEmpty(array)) {
|
if (isEmpty(array)) {
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
|
int i = Math.max(startIndexInclusive, 0);
|
||||||
int j = Math.min(array.length, endIndexExclusive) - 1;
|
int j = Math.min(array.length, endIndexExclusive) - 1;
|
||||||
int tmp;
|
int tmp;
|
||||||
while (j > i) {
|
while (j > i) {
|
||||||
@@ -3048,7 +3041,7 @@ public class ArrayUtil {
|
|||||||
if (isEmpty(array)) {
|
if (isEmpty(array)) {
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
|
int i = Math.max(startIndexInclusive, 0);
|
||||||
int j = Math.min(array.length, endIndexExclusive) - 1;
|
int j = Math.min(array.length, endIndexExclusive) - 1;
|
||||||
short tmp;
|
short tmp;
|
||||||
while (j > i) {
|
while (j > i) {
|
||||||
@@ -3085,7 +3078,7 @@ public class ArrayUtil {
|
|||||||
if (isEmpty(array)) {
|
if (isEmpty(array)) {
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
|
int i = Math.max(startIndexInclusive, 0);
|
||||||
int j = Math.min(array.length, endIndexExclusive) - 1;
|
int j = Math.min(array.length, endIndexExclusive) - 1;
|
||||||
char tmp;
|
char tmp;
|
||||||
while (j > i) {
|
while (j > i) {
|
||||||
@@ -3122,7 +3115,7 @@ public class ArrayUtil {
|
|||||||
if (isEmpty(array)) {
|
if (isEmpty(array)) {
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
|
int i = Math.max(startIndexInclusive, 0);
|
||||||
int j = Math.min(array.length, endIndexExclusive) - 1;
|
int j = Math.min(array.length, endIndexExclusive) - 1;
|
||||||
byte tmp;
|
byte tmp;
|
||||||
while (j > i) {
|
while (j > i) {
|
||||||
@@ -3159,7 +3152,7 @@ public class ArrayUtil {
|
|||||||
if (isEmpty(array)) {
|
if (isEmpty(array)) {
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
|
int i = Math.max(startIndexInclusive, 0);
|
||||||
int j = Math.min(array.length, endIndexExclusive) - 1;
|
int j = Math.min(array.length, endIndexExclusive) - 1;
|
||||||
double tmp;
|
double tmp;
|
||||||
while (j > i) {
|
while (j > i) {
|
||||||
@@ -3196,7 +3189,7 @@ public class ArrayUtil {
|
|||||||
if (isEmpty(array)) {
|
if (isEmpty(array)) {
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
|
int i = Math.max(startIndexInclusive, 0);
|
||||||
int j = Math.min(array.length, endIndexExclusive) - 1;
|
int j = Math.min(array.length, endIndexExclusive) - 1;
|
||||||
float tmp;
|
float tmp;
|
||||||
while (j > i) {
|
while (j > i) {
|
||||||
@@ -3233,7 +3226,7 @@ public class ArrayUtil {
|
|||||||
if (isEmpty(array)) {
|
if (isEmpty(array)) {
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
int i = startIndexInclusive < 0 ? 0 : startIndexInclusive;
|
int i = Math.max(startIndexInclusive, 0);
|
||||||
int j = Math.min(array.length, endIndexExclusive) - 1;
|
int j = Math.min(array.length, endIndexExclusive) - 1;
|
||||||
boolean tmp;
|
boolean tmp;
|
||||||
while (j > i) {
|
while (j > i) {
|
||||||
@@ -3844,9 +3837,7 @@ public class ArrayUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final Set<T> set = new LinkedHashSet<>(array.length, 1);
|
final Set<T> set = new LinkedHashSet<>(array.length, 1);
|
||||||
for (T t : array) {
|
Collections.addAll(set, array);
|
||||||
set.add(t);
|
|
||||||
}
|
|
||||||
return toArray(set, (Class<T>)getComponentType(array));
|
return toArray(set, (Class<T>)getComponentType(array));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2,6 +2,7 @@ package cn.hutool.core.util;
|
|||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.Console;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@@ -12,7 +13,6 @@ import cn.hutool.core.lang.Filter;
|
|||||||
* {@link ArrayUtil} 数组工具单元测试
|
* {@link ArrayUtil} 数组工具单元测试
|
||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class ArrayUtilTest {
|
public class ArrayUtilTest {
|
||||||
|
|
||||||
@@ -232,4 +232,27 @@ public class ArrayUtilTest {
|
|||||||
String[] distinct = ArrayUtil.distinct(array);
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user