📄 arrayutil.java
字号:
* sequentially equal to the elements of arrb. */ public static int countSameElements(byte[] arra, int start, byte[] arrb) { int k = 0; int limit = arra.length - start; if (limit > arrb.length) { limit = arrb.length; } for (int i = 0; i < limit; i++) { if (arra[i + start] == arrb[i]) { k++; } else { break; } } return k; } /** * Returns the index of the first occurence of arrb in arra. Or -1 if not found. */ public static int find(byte[] arra, int start, int limit, byte[] arrb) { int k = 0; limit = limit - arrb.length + 1; int value = arrb[0]; for (; k < limit; k++) { if (arra[k] == value) { if (arrb.length == 1) { return k; } if (containsAt(arra, k, arrb)) { return k; } } } return -1; } /** * Returns an index into arra (or -1) where the character is not in the * charset byte array. */ public static int findNotIn(byte[] arra, int start, int limit, byte[] charset) { int k = 0; for (; k < limit; k++) { for (int i = 0; i < charset.length; i++) { if (arra[k] == charset[i]) { continue; } } return k; } return -1; } /** * Returns an index into arra (or -1) where the character is in the * charset byte array. */ public static int findIn(byte[] arra, int start, int limit, byte[] charset) { int k = 0; for (; k < limit; k++) { for (int i = 0; i < charset.length; i++) { if (arra[k] == charset[i]) { return k; } } } return -1; } /** * Returns the index of b or c in arra. Or -1 if not found. */ public static int find(byte[] arra, int start, int limit, int b, int c) { int k = 0; for (; k < limit; k++) { if (arra[k] == b || arra[k] == c) { return k; } } return -1; } /** * Set elements of arrb true if their indexes appear in arrb. */ public static void intIndexesToBooleanArray(int[] arra, boolean[] arrb) { int k = 0; for (int i = 0; i < arra.length; i++) { if (arra[i] < arrb.length) { arrb[arra[i]] = true; } } } /** * Return true if for each true element in arrb, the corresponding * element in arra is true */ public static boolean containsAllTrueElements(boolean[] arra, boolean[] arrb) { for (int i = 0; i < arra.length; i++) { if (arrb[i] &&!arra[i]) { return false; } } return true; } /** * Returns true if arra from position start contains all elements of arrb * in sequential order. */ public static boolean containsAt(byte[] arra, int start, byte[] arrb) { return countSameElements(arra, start, arrb) == arrb.length; } /** * Returns the count of elements in arra from position start that are * among the elements of arrb. Stops at any element not in arrb. */ public static int countStartElementsAt(byte[] arra, int start, byte[] arrb) { int k = 0; mainloop: for (int i = start; i < arra.length; i++) { for (int j = 0; j < arrb.length; j++) { if (arra[i] == arrb[j]) { k++; continue mainloop; } } break; } return k; } /** * Returns the count of elements in arra from position start that are not * among the elements of arrb. * */ public static int countNonStartElementsAt(byte[] arra, int start, byte[] arrb) { int k = 0; mainloop: for (int i = start; i < arra.length; i++) { for (int j = 0; j < arrb.length; j++) { if (arra[i] == arrb[j]) { break mainloop; } } k++; } return k; } /** * Convenience wrapper for System.arraycopy(). */ public static void copyArray(Object source, Object dest, int count) { System.arraycopy(source, 0, dest, 0, count); } /** * Returns a range of elements of source from start to end of the array. */ public static int[] arraySlice(int[] source, int start, int count) { int[] slice = new int[count]; System.arraycopy(source, start, slice, 0, count); return slice; } /** * Fills the array with a value. */ public static void fillArray(Object[] array, Object value) { int to = array.length; while (--to >= 0) { array[to] = value; } } /** * Fills the int array with a value */ public static void fillArray(int[] array, int value) { int to = array.length; while (--to >= 0) { array[to] = value; } } /** * Returns a duplicates of an array. */ public static Object duplicateArray(Object source) { int size = Array.getLength(source); Object newarray = Array.newInstance(source.getClass().getComponentType(), size); System.arraycopy(source, 0, newarray, 0, size); return newarray; } /** * Returns the given array if newsize is the same as existing. * Returns a new array of given size, containing as many elements of * the original array as it can hold. */ public static Object resizeArrayIfDifferent(Object source, int newsize) { int oldsize = Array.getLength(source); if (oldsize == newsize) { return source; } Object newarray = Array.newInstance(source.getClass().getComponentType(), newsize); if (oldsize < newsize) { newsize = oldsize; } System.arraycopy(source, 0, newarray, 0, newsize); return newarray; } /** * Returns a new array of given size, containing as many elements of * the original array as it can hold. N.B. Always returns a new array * even if newsize parameter is the same as the old size. */ public static Object resizeArray(Object source, int newsize) { Object newarray = Array.newInstance(source.getClass().getComponentType(), newsize); int oldsize = Array.getLength(source); if (oldsize < newsize) { newsize = oldsize; } System.arraycopy(source, 0, newarray, 0, newsize); return newarray; } /** * Returns an array containing the elements of parameter source, with one * element removed or added. Parameter adjust {-1, +1} indicates the * operation. Parameter colindex indicates the position at which an element * is removed or added. Parameter addition is an Object to add when * adjust is +1. */ public static Object toAdjustedArray(Object source, Object addition, int colindex, int adjust) { int newsize = Array.getLength(source) + adjust; Object newarray = Array.newInstance(source.getClass().getComponentType(), newsize); copyAdjustArray(source, newarray, addition, colindex, adjust); return newarray; } /** * Copies elements of source to dest. If adjust is -1 the element at * colindex is not copied. If adjust is +1 that element is filled with * the Object addition. All the rest of the elements in source are * shifted left or right accordingly when they are copied. If adjust is 0 * the addition is copied over the element at colindex. * * No checks are perfomed on array sizes and an exception is thrown * if they are not consistent with the other arguments. */ public static void copyAdjustArray(Object source, Object dest, Object addition, int colindex, int adjust) { int length = Array.getLength(source); if (colindex < 0) { System.arraycopy(source, 0, dest, 0, length); return; } System.arraycopy(source, 0, dest, 0, colindex); if (adjust == 0) { int endcount = length - colindex - 1; Array.set(dest, colindex, addition); if (endcount > 0) { System.arraycopy(source, colindex + 1, dest, colindex + 1, endcount); } } else if (adjust < 0) { int endcount = length - colindex - 1; if (endcount > 0) { System.arraycopy(source, colindex + 1, dest, colindex, endcount); } } else { int endcount = length - colindex; Array.set(dest, colindex, addition); if (endcount > 0) { System.arraycopy(source, colindex, dest, colindex + 1, endcount); } } } /** * Returns a new array with the elements in collar adjusted to reflect * changes at colindex. <p> * * Each element in collarr represents an index into another array * otherarr. <p> * * colindex is the index at which an element is added or removed. * Each element in the result array represents the new, * adjusted index. <p> * * For each element of collarr that represents an index equal to * colindex and adjust is -1, the result will not contain that element * and will be shorter than collar by one element. * * @param colarr the source array * @param colindex index at which to perform adjustement * @param adjust +1, 0 or -1 * @return new, adjusted array */ public static int[] toAdjustedColumnArray(int[] colarr, int colindex, int adjust) { if (colarr == null) { return null; } int[] intarr = new int[colarr.length]; int j = 0; for (int i = 0; i < colarr.length; i++) { if (colarr[i] > colindex) { intarr[j] = colarr[i] + adjust; j++; } else if (colarr[i] == colindex) { if (adjust < 0) { // skip an element from colarr } else { intarr[j] = colarr[i] + adjust; j++; } } else { intarr[j] = colarr[i]; j++; } } if (colarr.length != j) { int[] newarr = new int[j]; copyArray(intarr, newarr, j); return newarr; } return intarr; } /** * Copies some elements of row into colobject by using colindex as * the list of indexes into row. <p> * * colindex and colobject are of equal length and are normally * shorter than row. <p> * * @param row the source array * @param colindex the list of indexes into row * @param colobject the destination array */ public static void copyColumnValues(Object[] row, int[] colindex, Object[] colobject) { for (int i = 0; i < colindex.length; i++) { colobject[i] = row[colindex[i]]; } } public static void copyColumnValues(int[] row, int[] colindex, int[] colobject) { for (int i = 0; i < colindex.length; i++) { colobject[i] = row[colindex[i]]; } } public static void fillSequence(int[] colindex) { for (int i = 0; i < colindex.length; i++) { colindex[i] = i; } }/* public static void main(String[] args) { int[] a = new int[] { 23, 11, 37, 7, 1, 5 }; int[] b = new int[] { 1, 3, 7, 11, 13, 17, 19, 3, 1 }; int[] c = toAdjustedColumnArray(a, 7, -1); int[] d = toAdjustedColumnArray(b, 11, 1); int[] e = new int[a.length]; copyArray(a, e, a.length); sortArray(e); int[] f = new int[b.length]; copyArray(b, f, b.length); sortArray(f); boolean x = haveEqualSets(a, e, a.length); boolean y = haveEqualSets(b, f, b.length); System.out.print("test passed: "); System.out.print(x == true && y == true && c.length == a.length - 1 && d.length == b.length); }*/}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -