一个好用的api

    /**
     * Convert the given array (which may be a primitive array) to an
     * object array (if necessary of primitive wrapper objects).
     * <p>A {@code null} source value will be converted to an
     * empty Object array.
     * @param source the (potentially primitive) array
     * @return the corresponding object array (never {@code null})
     * @throws IllegalArgumentException if the parameter is not an array
     */
    public static Object[] toObjectArray(@Nullable Object source) {
        if (source instanceof Object[]) {
            return (Object[]) source;
        }
        if (source == null) {
            return new Object[0];
        }
        if (!source.getClass().isArray()) {
            throw new IllegalArgumentException("Source is not an array: " + source);
        }
        int length = Array.getLength(source);
        if (length == 0) {
            return new Object[0];
        }
        Class<?> wrapperType = Array.get(source, 0).getClass();
        Object[] newArray = (Object[]) Array.newInstance(wrapperType, length);
        for (int i = 0; i < length; i++) {
            newArray[i] = Array.get(source, i);
        }
        return newArray;
    }

org.springframework.util.ObjectUtils#toObjectArray

 

Arrays.stream(ObjectUtils.toObjectArray(new byte[]{1,2,3})).map(String::valueOf).collect(Collectors.joining(","))

 

相关文章:

  • 2021-07-06
  • 2021-12-15
  • 2021-10-09
  • 2021-11-20
  • 2021-10-31
  • 2022-12-23
  • 2021-12-03
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-09
  • 2021-08-23
  • 2022-12-23
  • 2022-01-31
  • 2021-11-19
相关资源
相似解决方案