java 数组工具类 ArrayUtil

/**
 * 数组工具类
 * 
 * @author shenggm
 * 
 */
public final class ArrayUtil {
    private ArrayUtil() {
    }

    public static final boolean[] TRUE = { true };
    public static final boolean[] FALSE = { false };
    public static final String[] EMPTY_STRING_ARRAY = {};
    public static final int[] EMPTY_INT_ARRAY = {};
    public static final boolean[] EMPTY_BOOLEAN_ARRAY = {};
    @SuppressWarnings("rawtypes")
    public static final Class[] EMPTY_CLASS_ARRAY = {};
    public static final Object[] EMPTY_OBJECT_ARRAY = {};
    public static final byte[] EMPTY_BYTE_ARRAY = {};
    private static int SEED = 23;
    private static int PRIME_NUMER = 37;

    @SuppressWarnings("unchecked")
    public static <T> T[] fill(T value, int length) {
        T[] result = (T[]) Array.newInstance(value.getClass(), length);
        Arrays.fill(result, value);
        return result;
    }

    private static int hash(int seed, int i) {
        return PRIME_NUMER * seed + i;
    }

    public static String[] slice(String[] strings, int begin, int length) {
        String[] result = new String[length];
        System.arraycopy(strings, begin, result, 0, length);
        return result;
    }

    /**
     * calculate the array hash (only the first level)
     */
    public static int hash(Object[] array) {
        int length = array.length;
        int seed = SEED;
        for (int index = 0; index < length; index++) {
            seed = hash(seed, array[index] == null ? 0 : array[index].hashCode());
        }
        return seed;
    }

    /**
     * calculate the array hash (only the first level)
     */
    public static int hash(byte[] bytes) {
        int length = bytes.length;
        int seed = SEED;
        for (int index = 0; index < length; index++) {
            seed = hash(seed, bytes[index]);
        }
        return seed;
    }

    public static char[] fill(char value, int length) {
        char[] result = new char[length];
        Arrays.fill(result, value);
        return result;
    }

    public static boolean[] fill(boolean value, int length) {
        boolean[] result = new boolean[length];
        Arrays.fill(result, value);
        return result;
    }

    public static byte[] fill(byte value, int length) {
        byte[] result = new byte[length];
        Arrays.fill(result, value);
        return result;
    }

    public static double[] fill(double value, int length) {
        double[] result = new double[length];
        Arrays.fill(result, value);
        return result;
    }

    public static float[] fill(float value, int length) {
        float[] result = new float[length];
        Arrays.fill(result, value);
        return result;
    }

    public static long[] fill(long value, int length) {
        long[] result = new long[length];
        Arrays.fill(result, value);
        return result;
    }

    public static int[] fill(int value, int length) {
        int[] result = new int[length];
        Arrays.fill(result, value);
        return result;
    }

    public static short[] fill(short value, int length) {
        short[] result = new short[length];
        Arrays.fill(result, value);
        return result;
    }

    @SuppressWarnings({ "unchecked" })
    public static <T> T[] join(T[] x, T[] y) {
        T[] result = (T[]) Array.newInstance(x.getClass().getComponentType(), x.length + y.length);
        System.arraycopy(x, 0, result, 0, x.length);
        System.arraycopy(y, 0, result, x.length, y.length);
        return result;
    }

    public static boolean[] join(boolean[] x, boolean[] y) {
        boolean[] result = new boolean[x.length + y.length];
        System.arraycopy(x, 0, result, 0, x.length);
        System.arraycopy(y, 0, result, x.length, y.length);
        return result;
    }

    public static byte[] join(byte[] x, byte[] y) {
        byte[] result = new byte[x.length + y.length];
        System.arraycopy(x, 0, result, 0, x.length);
        System.arraycopy(y, 0, result, x.length, y.length);
        return result;
    }

    public static char[] join(char[] x, char[] y) {
        char[] result = new char[x.length + y.length];
        System.arraycopy(x, 0, result, 0, x.length);
        System.arraycopy(y, 0, result, x.length, y.length);
        return result;
    }

    public static double[] join(double[] x, double[] y) {
        double[] result = new double[x.length + y.length];
        System.arraycopy(x, 0, result, 0, x.length);
        System.arraycopy(y, 0, result, x.length, y.length);
        return result;
    }

    public static float[] join(float[] x, float[] y) {
        float[] result = new float[x.length + y.length];
        System.arraycopy(x, 0, result, 0, x.length);
        System.arraycopy(y, 0, result, x.length, y.length);
        return result;
    }

    public static long[] join(long[] x, long[] y) {
        long[] result = new long[x.length + y.length];
        System.arraycopy(x, 0, result, 0, x.length);
        System.arraycopy(y, 0, result, x.length, y.length);
        return result;
    }

    public static int[] join(int[] x, int[] y) {
        int[] result = new int[x.length + y.length];
        System.arraycopy(x, 0, result, 0, x.length);
        System.arraycopy(y, 0, result, x.length, y.length);
        return result;
    }

    public static short[] join(short[] x, short[] y) {
        short[] result = new short[x.length + y.length];
        System.arraycopy(x, 0, result, 0, x.length);
        System.arraycopy(y, 0, result, x.length, y.length);
        return result;
    }

    public static String[] join(String[] x, String[] y, boolean[] use) {
        String[] result = new String[x.length + countTrue(use)];
        System.arraycopy(x, 0, result, 0, x.length);
        int k = x.length;
        for (int i = 0; i < y.length; i++) {
            if (use[i]) {
                result[k++] = y[i];
            }
        }
        return result;
    }

//     public static String[] add(String[] x, String y) {
//          if (StringUtil.isNullOrEmpty(y))
//               return x;
//          int size = x.length;
//          String[] result = new String[size + 1];
//          System.arraycopy(x, 0, result, 0, size);
//          result[size] = y;
//          return result;
//     }
    @SuppressWarnings({ "unchecked" })
    public static <T> T[] add(T[] x, T y) {
        if (null == y)
            return x;
        T[] result = (T[]) Array.newInstance(x.getClass().getComponentType(), x.length + 1);
        System.arraycopy(x, 0, result, 0, x.length);
        result[x.length] = y;
        return result;
    }

    /**
     * Check whether the given array contains the given element.
     * 
     * @param array   the array to check (may be <code>null</code>, in which case the return value will always be <code>false</code>)
     * @param element the element to check for
     * @return whether the element has been found in the given array
     */
    public static <T> boolean contains(T[] array, T element) {
        if (array == null)
            return false;
        for (int i = 0; i < array.length; i++) {
            if (array[i].equals(element)) {
                return true;
            }
        }
        return false;
    }

    public static boolean contains(boolean[] array, boolean element) {
        if (array == null)
            return false;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == element) {
                return true;
            }
        }
        return false;
    }

    public static boolean contains(byte[] array, byte element) {
        if (array == null)
            return false;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == element) {
                return true;
            }
        }
        return false;
    }

    public static boolean contains(char[] array, char element) {
        if (array == null)
            return false;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == element) {
                return true;
            }
        }
        return false;
    }

    public static boolean contains(double[] array, double element) {
        if (array == null)
            return false;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == element) {
                return true;
            }
        }
        return false;
    }

    public static boolean contains(float[] array, float element) {
        if (array == null)
            return false;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == element) {
                return true;
            }
        }
        return false;
    }

    public static boolean contains(long[] array, long element) {
        if (array == null)
            return false;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == element) {
                return true;
            }
        }
        return false;
    }

    public static boolean contains(int[] array, int element) {
        if (array == null)
            return false;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == element) {
                return true;
            }
        }
        return false;
    }

    public static boolean contains(short[] array, short element) {
        if (array == null)
            return false;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == element) {
                return true;
            }
        }
        return false;
    }

    public static <T> int indexOf(T[] array, T element) {
        if (array == null)
            return -1;
        for (int i = 0; i < array.length; i++) {
            if (array[i].equals(element)) {
                return i;
            }
        }
        return -1;
    }

    public static int indexOf(boolean[] array, boolean element) {
        if (array == null)
            return -1;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == element) {
                return i;
            }
        }
        return -1;
    }

    public static int indexOf(byte[] array, byte element) {
        if (array == null)
            return -1;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == element) {
                return i;
            }
        }
        return -1;
    }

    public static int indexOf(char[] array, char element) {
        if (array == null)
            return -1;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == element) {
                return i;
            }
        }
        return -1;
    }

    public static int indexOf(double[] array, double element) {
        if (array == null)
            return -1;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == element) {
                return i;
            }
        }
        return -1;
    }

    public static int indexOf(float[] array, float element) {
        if (array == null)
            return -1;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == element) {
                return i;
            }
        }
        return -1;
    }

    public static int indexOf(long[] array, long element) {
        if (array == null)
            return -1;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == element) {
                return i;
            }
        }
        return -1;
    }

    public static int indexOf(int[] array, int element) {
        if (array == null)
            return -1;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == element) {
                return i;
            }
        }
        return -1;
    }

    public static int indexOf(short[] array, short element) {
        if (array == null)
            return -1;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == element) {
                return i;
            }
        }
        return -1;
    }

    /**
     * Return a String representation of the specified Object.
     * <p>
     * Builds a String representation of the contents in case of an array. Returns <code>"null"</code> if <code>obj</code> is <code>null</code>.
     * 
     * @param obj the object to build a String representation for
     * @return a String representation of <code>obj</code>
     */
    public static String toString(Object obj) {
        if (obj == null) {
            return "null";
        }
        if (obj instanceof String) {
            return (String) obj;
        }
        if (obj instanceof Object[]) {
            return Arrays.toString((Object[]) obj);
        }
        if (obj instanceof boolean[]) {
            return Arrays.toString((boolean[]) obj);
        }
        if (obj instanceof byte[]) {
            return Arrays.toString((byte[]) obj);
        }
        if (obj instanceof char[]) {
            return Arrays.toString((char[]) obj);
        }
        if (obj instanceof double[]) {
            return Arrays.toString((double[]) obj);
        }
        if (obj instanceof float[]) {
            return Arrays.toString((float[]) obj);
        }
        if (obj instanceof int[]) {
            return Arrays.toString((int[]) obj);
        }
        if (obj instanceof long[]) {
            return Arrays.toString((long[]) obj);
        }
        if (obj instanceof short[]) {
            return Arrays.toString((short[]) obj);
        }
        String str = obj.toString();
        return (str != null ? str : "");
    }

    public static boolean isAllTrue(boolean[] array) {
        for (int i = 0; i < array.length; i++) {
            if (!array[i]) {
                return false;
            }
        }
        return true;
    }

    public static boolean isAllFalse(boolean[] array) {
        for (int i = 0; i < array.length; i++) {
            if (array[i]) {
                return false;
            }
        }
        return true;
    }

    public static boolean isAllNegative(int[] array) {
        for (int i = 0; i < array.length; i++) {
            if (array[i] >= 0) {
                return false;
            }
        }
        return true;
    }

    public static int countTrue(boolean[] array) {
        int result = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i]) {
                result++;
            }
        }
        return result;
    }

    @SuppressWarnings("rawtypes")
    public static Object[] copy(Class elementClass, Object[] array) {
        Object[] result = (Object[]) Array.newInstance(elementClass, array.length);
        System.arraycopy(array, 0, result, 0, array.length);
        return result;
    }

    public static <T> T[] copy(T[] array) {
        return Arrays.copyOf(array, array.length);
    }

    /**
     * Convert the given array (which may be a primitive array) to an object array (if necessary of primitive wrapper objects).
     * <p>
     * A <code>null</code> source value will be converted to an empty Object array.
     * 
     * @param source the (potentially primitive) array
     * @return the corresponding object array (never <code>null</code>)
     * @throws IllegalArgumentException if the parameter is not an array
     */
    @SuppressWarnings("rawtypes")
    public static Object[] toObjectArray(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;
    }

    public static String[] toStringArray(Object[] objects) {
        int length = objects.length;
        String[] result = new String[length];
        for (int i = 0; i < length; i++) {
            result[i] = objects[i].toString();
        }
        return result;
    }

    public static Object[] typecast(Object[] array, Object[] to) {
        return java.util.Arrays.asList(array).toArray(to);
    }

    /**
     * Convert the supplied array into a List. A primitive array gets converted into a List of the appropriate wrapper type.
     * <p>
     * A <code>null</code> source value will be converted to an empty List.
     * 
     * @param source the (potentially primitive) array
     * @return the converted List result
     * @see ObjectUtil#toObjectArray(Object)
     */
    @SuppressWarnings("rawtypes")
    public static List toList(Object source) {
        return Arrays.asList(toObjectArray(source));
    }

    public static <T> List<T> toList(T... source) {
        return Arrays.asList(source);
    }

    public static int toInt(byte[] bytes) {
        int result = 0;
        for (int i = 0; i < 4; i++) {
            result = ((result << 8) - Byte.MIN_VALUE) + bytes[i];
        }
        return result;
    }

    public static String toString(Object[] array) {
        int len = array.length;
        if (len == 0) {
            return "";
        }
        StringBuffer buf = new StringBuffer(len * 12);
        for (int i = 0; i < (len - 1); i++) {
            buf.append(array[i]).append(", ");
        }
        return buf.append(array[len - 1]).toString();
    }

    public static String toHexString(byte[] b) {
        StringBuffer sb = new StringBuffer(b.length * 2);
        for (int i = 0; i < b.length; i++) {
            int v = b[i] & 0xff;
            if (v < 16) {
                sb.append('0');
            }
            sb.append(Integer.toHexString(v));
        }
        return sb.toString().toUpperCase();
    }

    public static String[] remove(String[] array, String y) {
        if (ObjectUtil.isNullOrEmpty(array) || StringUtil.isNullOrEmpty(y)) {
            return array;
        }
        Set<String> set = new TreeSet<String>();
        for (String element : array) {
            if (!element.equals(y)) {
                set.add(element);
            }
            ;
        }
        return StringUtil.toStringArray(set);
    }

    /**
     * Remove duplicate Strings from the given array. Also sorts the array, as it uses a TreeSet.
     * 
     * @param array the String array
     * @return an array without duplicates, in natural sort order
     */
    public static String[] removeDuplicateStrings(String[] array) {
        if (ObjectUtil.isNullOrEmpty(array)) {
            return array;
        }
        Set<String> set = new TreeSet<String>();
        for (String element : array) {
            set.add(element);
        }
        return StringUtil.toStringArray(set);
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static Object getMaxValue(List<Object> valueList) {
        if (ObjectUtil.isNullOrEmpty(valueList)) {
            return null;
        }
        Map<String, Integer> countMap = new Hashtable();
        Map<String, Object> objectMap = new Hashtable();
        String key = null;
        Integer count = 0;
        for (Object o : valueList) {
            key = o.toString();
            if (!objectMap.containsKey(key)) {
                objectMap.put(key, o);
                countMap.put(key, 1);
            } else {
                count = countMap.get(key);
                countMap.put(key, count + 1);
            }
        }
        count = 0;
        for (String k : countMap.keySet()) {
            if (countMap.get(k) > count) {
                count = countMap.get(k);
                key = k;
            }
        }
        return objectMap.get(key);
    }

    /**
     * Compare 2 arrays only at the first level
     */
    public static boolean isEquals(Object[] o1, Object[] o2) {
        if (o1 == o2)
            return true;
        if (o1 == null || o2 == null)
            return false;
        int length = o1.length;
        if (length != o2.length)
            return false;
        for (int index = 0; index < length; index++) {
            if (!o1[index].equals(o2[index]))
                return false;
        }
        return true;
    }

    /**
     * Compare 2 arrays only at the first level
     */
    public static boolean isEquals(char[] o1, char[] o2) {
        if (o1 == o2)
            return true;
        if (o1 == null || o2 == null)
            return false;
        int length = o1.length;
        if (length != o2.length)
            return false;
        for (int index = 0; index < length; index++) {
            if (!(o1[index] == o2[index]))
                return false;
        }
        return true;
    }

    /**
     * Compare 2 arrays only at the first level
     */
    public static boolean isEquals(byte[] b1, byte[] b2) {
        if (b1 == b2)
            return true;
        if (b1 == null || b2 == null)
            return false;
        int length = b1.length;
        if (length != b2.length)
            return false;
        for (int index = 0; index < length; index++) {
            if (!(b1[index] == b2[index]))
                return false;
        }
        return true;
    }
}

package com.xj.hhjk.common.util;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
/** * @project: WebProjectUtil * @class: NumberUtil * @describe: 此工具类用来处理数字方面的逻辑, * 如返回指定位数的随机数字、Double的加减乘除精确运算、指定位数数字用“0”补齐 * @autho: Administrator * @date: 2013-6-7 下午02:26:27 * @alter: Administrator * @alterDate: 2013-6-7 下午02:26:27 * @alterRemark: * @version V1.0 */ public class NumberUtil { private static final int DEF_DIV_SCALE = 2; /** * @return 返回12位随机数 */ public static String randomNumber() { } /** * @param parm * @return 返回指定位数随机数 */ public static String randomNumber(int parm) { } /** * * 两个Double数相加 * * @param v1 * @param v2 * @return Double */ public static Double add(Double v1, Double v2) { } /** * * 两个Double数相减 * * @param v1 * @param v2 * @return Double */ public static Double sub(Double v1, Double v2) { } /** * * 两个Double数相乘 * * @param v1 * @param v2 * @return Double */ public static Double mul(Double v1, Double v2) { } /** * * 两个Double数相除 * * @param v1 * @param v2 * @return Double */ public static Double div(Double v1, Double v2) { } /** * * 两个Double数相除,并保留scale位小数 * * @param v1 * @param v2 * @param scale * @return Double */ public static Double div(Double v1, Double v2, int scale) { } /** * 返回指定Double的负数 * @param v1 * @return */ public static Double neg(Double v1) { /** * @Title: toFixdLengthString * @Description: 将字符串用符号填充位数 * @param str 源字符串 * @param fixdlenth 位数 * @return String * @throws */ public static String toFixdLengthString(String str, int fixdlenth) { } /** * @Title: toFixdLengthString * @Description: 将数字用“0”填充位数 * @param num * @param fixdlenth * @return String * @throws */ public static String toFixdLengthString(int num, int fixdlenth) { } /** * @Title: generateSpaceString * @Description: 得到指定位数占位符 * @param length * @return String * @throws */ public static String generateSpaceString(int length) { } /** * @Title: generateZeroString * @Description: 得到指定位数的“0”的占位符 * @param length * @return String * @throws */ public static String generateZeroString(int length) { } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长青风

赏一块,发大财!赏两块,惹人爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值