All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.t.util.common.ArrayUtil Maven / Gradle / Ivy

There is a newer version: 1.0.16.RELEASE
Show newest version
package cn.t.util.common;

import java.util.Arrays;
import java.util.List;

/**
 *
 */
public class ArrayUtil {

    /**
     * 数组合并
     * @param bytesArrays xxx
     * @return xxx
     */
    public static byte[] combine(byte[]... bytesArrays) {
        if (bytesArrays == null) {
            return null;
        } else if (bytesArrays.length == 0) {
            return new byte[0];
        } else {
            byte[] result = null;
            for (byte[] bs : bytesArrays) {
                if (result == null) {
                    result = bs;
                } else {
                    if (bs != null && bs.length > 0) {
                        int startIndex = result.length;
                        result = Arrays.copyOf(result, result.length + bs.length);
                        System.arraycopy(bs, 0, result, startIndex, bs.length);
                    }
                }
            }
            return result;
        }
    }

    /**
     * 数组检索
     * @param source xxx
     * @param target xxx
     * @return xxx
     */
    public static int binarySearch(byte[] source, byte[] target) {
        return binarySearch(source, 0, source.length,
            target, 0, target.length, 0);
    }

    /**
     * 数组反转
     *
     * @param bytes xxx
     * @return xxx
     * */
    public static byte[] reverse(byte[] bytes) {
        if(bytes == null || bytes.length < 2) {
            return bytes;
        } else {
            byte [] newArray = new byte[bytes.length];
            for(int i=0; i= sourceCount) {
            return (targetCount == 0 ? sourceCount : -1);
        }
        if (fromIndex < 0) {
            fromIndex = 0;
        }
        if (targetCount == 0) {
            return fromIndex;
        }

        byte first = target[targetOffset];
        int max = sourceOffset + (sourceCount - targetCount);

        for (int i = sourceOffset + fromIndex; i <= max; i++) {
            /* Look for first character. */
            if (source[i] != first) {
                while (++i <= max && source[i] != first) ;
            }

            /* Found first character, now look at the rest of v2 */
            if (i <= max) {
                int j = i + 1;
                int end = j + targetCount - 1;
                for (int k = targetOffset + 1; j < end && source[j]
                    == target[k]; j++, k++)
                    ;

                if (j == end) {
                    /* Found whole string. */
                    return i - sourceOffset;
                }
            }
        }
        return -1;
    }

    public static byte[] add(byte[] arr, byte... data) {
        if(data != null && data.length > 0) {
            byte[] arrNew = Arrays.copyOf(arr, arr.length + data.length);
            System.arraycopy(data, 0, arrNew, arr.length, data.length);
            arr = arrNew;
        }
        return arr;
    }

    public static int[] convertToIntArray(List integerList) {
        if(CollectionUtil.isEmpty(integerList)) {
            return new int[0];
        } else {
            return integerList.stream().mapToInt(i->i).toArray();
        }
    }

    public static boolean isEmpth(Object[] arr) {
        return arr == null || arr.length==0;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy