📄 arrays.java
字号:
+ " char= " + escapedC; throw new IllegalArgumentException(msg); } } } result.add(csvToArray(csvs.substring(start))); return (String[][]) result.toArray(new String[result.size()][]); } /** * Converts the two-dimensional array of strings to comma-separated * value notaiton. See {@link #csvToArray2D(String)} for more * information on the encoding. The input may be a ragged array. * * @param elts Array of arrays to encode. * @return Comma-separated values representation of the array. */ public static String arrayToCSV(String[][] elts) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < elts.length; ++i) { if (i > 0) sb.append('\n'); arrayToCSV(elts[i],sb); } return sb.toString(); } /** * Converts an array of objects to comma-separated values * notation. Each object will be converted to a string using its * <code>toString()</code> method. * * <p>See {@link #csvToArray(String)} for information on * the encoding and the reverse mapping. * * @param xs Array of strings to encode. * @return CSV-encoded array of strings. */ public static String arrayToCSV(Object[] xs) { StringBuffer sb = new StringBuffer(); arrayToCSV(xs,sb); return sb.toString(); } static void arrayToCSV(Object[] xs, StringBuffer sb) { for (int i = 0; i < xs.length; ++i) { if (i > 0) sb.append(','); csvEncode(xs[i].toString(), sb); } } static final char BACKSLASH_CHAR = '\\'; static void csvEncode(String in, StringBuffer sb) { for (int i = 0; i < in.length(); ++i) { char c = in.charAt(i); if (csvEscape(c)) sb.append(BACKSLASH_CHAR); sb.append(c); } } static boolean csvEscape(char c) { return c == BACKSLASH_CHAR || c == ',' || c == '\n'; } /** * Returns true if the specified object is an element of the * specified array. Returns <code>false</code> if the specified * array is null. * * @param x Object to test for membership. * @param xs Array to test for object. * @return <code>true</code> if the specified object is an element * of the specified array. */ public static boolean member(Object x, Object[] xs) { if (xs == null) return false; for (int i = xs.length; --i >= 0; ) { if (xs[i] == null) continue; if (xs[i].equals(x)) return true; } return false; } /** * Returns true if the specified character is a member of the * specified array. Returns <code>false</code> if the specified * array is null. * * @param c Character to test for membership. * @param cs Array to test for character. * @return <code>true</code> if the specified character is an * element of the specified array. */ public static boolean member(char c, char[] cs) { if (cs == null) return false; for (int i = 0; i < cs.length; ++i) { if (cs[i] == c) return true; } return false; } /** * Returns the concatenation of the string representations of the * specified objects separated by commas, with the whole * surrounded by square brackets and separated by a comma. * * @param xs Array of which to return a string representation. * @return String representation of the specified array. */ public static String arrayToString(Object[] xs) { StringBuffer sb = new StringBuffer(); arrayToStringBuffer(sb,xs); return sb.toString(); } /** * Appends to the string buffer the concatenation of the string * representations of the specified objects separated by commas, * with the whole surrounded by square brackets and separated by a * comma. * * @param sb String buffer to which string representation is * appended. * @param xs Array of which to return a string representation. */ public static void arrayToStringBuffer(StringBuffer sb, Object[] xs) { sb.append('['); for (int i = 0; i < xs.length; ++i) { if (i > 0) sb.append(','); sb.append(xs[i]); } sb.append(']'); } /** * Returns the array of characters consisting of the members of * the first specified array followed by the specified character. * * @param cs Characters to start resulting array. * @param c Last character in resulting array. * @return Array of characters consisting of the characters in the * first array followed by the last character. * @throws NullPointerException If the array of characters is * null. */ public static char[] concatenate(char[] cs, char c) { char[] result = new char[cs.length+1]; for (int i = 0; i < cs.length; ++i) result[i] = cs[i]; result[result.length-1] = c; return result; } /** * Returns a new array of strings containing the elements of the * first array of strings specified followed by the elements of * the second array of strings specified. * * @param xs First array of strings. * @param ys Second array of strings. * @return Concatenation of first array of strings followed by the * second array of strings. */ public static String[] concatenate(String[] xs, String[] ys) { String[] result = new String[xs.length + ys.length]; System.arraycopy(xs,0,result,0,xs.length); System.arraycopy(ys,0,result,xs.length,ys.length); return result; } /** * Returns the sum of the specified integer array. Note that * there is no check for overflow. * * @param xs Array of integers to sum. * @return Sum of the array. */ public static int sum(int[] xs) { int sum = 0; for (int i = 0; i < xs.length; ++i) sum += xs[i]; return sum; } /** * Return <code>true</code> if the specified arrays are * the same length and contain the same elements. * * @param xs First array. * @param ys Second array. * @return <code>true</code> if the specified arrays are the same * length and contain the same elements. */ public static boolean equals(Object[] xs, Object[] ys) { if (xs.length != ys.length) return false; for (int i = 0; i < xs.length; ++i) if (!xs[i].equals(ys[i])) return false; return true; } /** * Randomly permutes the elements of the specified array using * a freshly generated randomizer. The * resulting array will have the same elements, but arranged into * a (possibly) different order. * * @param xs Array to permute. */ public static <E> void permute(E[] xs) { permute(xs,new Random()); } /** * Randomly permutes the elements of the specified array using the * specified randomizer. The resulting array will have the same * elements, but arranged into a (possibly) different order. * * @param xs Array to permute. * @param random Randomizer to use for permuation. */ public static <E> void permute(E[] xs, Random random) { for (int i = xs.length; --i > 0; ) { int pos = random.nextInt(i); E temp = xs[pos]; xs[pos] = xs[i]; xs[i] = temp; } } /** * Randomly permutes the elements of the specified integer array * using a newly created randomizer. The resulting array will * have the same elements, but arranged into a (possibly) * different order. The randomizer is created with a call to * the nullary constructor {@link java.util.Random#Random()}. * * @param xs Array to permute. */ public static void permute(int[] xs) { permute(xs, new Random()); } /** * Randomly permutes the elements of the specified integer * array using the specified randomizer. * * @param xs Array to permute. * @param random Randomizer to use for permutations. */ public static void permute(int[] xs, Random random) { for (int i = xs.length; --i > 0; ) { int pos = random.nextInt(i); int temp = xs[pos]; xs[pos] = xs[i]; xs[i] = temp; } } /** * A length <code>0</code> array of integers. */ public static final int[] EMPTY_INT_ARRAY = new int[] { }; /** *A length <code>0</code> array of characters. */ public static final char[] EMPTY_CHAR_ARRAY = new char[0];}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -