⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 arrays.java

📁 一个自然语言处理的Java开源工具包。LingPipe目前已有很丰富的功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * LingPipe v. 3.5 * Copyright (C) 2003-2008 Alias-i * * This program is licensed under the Alias-i Royalty Free License * Version 1 WITHOUT ANY WARRANTY, without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the Alias-i * Royalty Free License Version 1 for more details. * * You should have received a copy of the Alias-i Royalty Free License * Version 1 along with this program; if not, visit * http://alias-i.com/lingpipe/licenses/lingpipe-license-1.txt or contact * Alias-i, Inc. at 181 North 11th Street, Suite 401, Brooklyn, NY 11211, * +1 (718) 290-9170. */package com.aliasi.util;import java.util.ArrayList;import java.util.Iterator;import java.util.NoSuchElementException;import java.util.Random;/** * Static utility methods for processing arrays. * * @author  Bob Carpenter * @version 3.6 * @since   LingPipe1.0 */public class Arrays {    /**     * Forbid instance construction.     */    private Arrays() {        /* no instances */    }    /**     * Returns a copy of the specified array of integers of the     * specified size.  As many of the original elements as will     * fit in the new array are copied and the remaining elements     * are set to zero.     *     * @param xs Original array.     * @param newSize Length of returned array.     * @return New array of specified length with as many elements     * copied from the original array as will fit.     */    public static int[] reallocate(int[] xs, int newSize) {        int[] ys = new int[newSize];        for (int i = 0; i < xs.length && i < newSize; ++i)            ys[i] = xs[i];        return ys;    }    /**     * Reallocates the specified integer array to be 50 percent     * longer, with a minimum growth in length of one element.  All of     * the elements of the specified array will be copied into the     * resulting array and the remaining elements initialized to zero     * (<code>0</code>).     *     * @param xs Array to reallocate.     * @return Result of reallocation.     */    public static int[] reallocate(int[] xs) {        int len = (xs.length * 3) / 2;        return reallocate(xs,len == xs.length ? xs.length + 1 : len);    }    /**     * Return the result of adding the specified character to the     * specified sorted character array.  The original array will     * be returned if the character is in the array, otherwise a     * new array will be constructed and returned.     *     * <p><i>Warning:</i> No check is done that the incoming character     * array is in order.     *     * @param c Character to add.     * @param cs Array of characters in sorted order.     * @return The result of adding the character to the array in the     * correct order, returning a larger array if necessary.     */    public static char[] add(char c, char[] cs) {        if (java.util.Arrays.binarySearch(cs,c) >= 0)            return cs;        char[] result = new char[cs.length+1];        int i = 0;        while (i < cs.length && c > cs[i]) {            result[i] = cs[i];            ++i;        }        result[i] = c;        ++i;        while (i < result.length) {            result[i] = cs[i-1];            ++i;        }        return result;    }    /**     * Return a shallow copy of the specified array that     * contains the same elements as the specified array.  If     * the input is <code>null</code>, then <code>null</code>     * is returned.     *     * @param cs Array to copy.     * @return Shallow copy of array.     */    public static char[] copy(char[] cs) {        if (cs == null) return null;        char[] cs2 = new char[cs.length];        for (int i = 0; i < cs.length; ++i)            cs2[i] = cs[i];        return cs2;    }    /**     * Converts the specified character sequence to an array     * of characters.     *     * @param cSeq Character sequence to convert.     * @return Array of characters in sequence.     */    public static char[] toArray(CharSequence cSeq) {        // return cSeq.toString().toCharArray();        char[] cs = new char[cSeq.length()];        for (int i = 0; i < cs.length; ++i)            cs[i] = cSeq.charAt(i);        return cs;    }    /**     * Converts a string of comma-separated values into an array of     * strings.  The returned array will be at least one element long,     * with values being the maximal-length strings between commas.     * Values may be of length zero.     *     * <p>Any comma (<code>,</code>), backslash (<code>\</code>) or     * newline (<code>\n</code>) that appears in a value is escaped     * with a backslash (<code>\</code>).  No other use of backslash     * or comma in values is permitted.  An attempt to decode an     * ill-formed input will throw an exception.     *     * <p>Some examples of strings and arrays they return, using     * Java's string-literal escapes, are:     *     * <p>     * <table cellpadding="3" border="1">     * <tr><td><i>CSV</i></td>     *     <td><i>String</i></td></tr>     * <tr><td><code>""</code></td>     *     <td><code>{ "" }</code></td></tr>     * <tr><td><code>"a"</code></td>     *     <td><code>{ "a" }</code></td></tr>     * <tr><td><code>"a,b"</code></td>     *     <td><code>{ "a", "b" }</code></td></tr>     * <tr><td><code>"abc,def,g"</code></td>     *     <td><code>{ "abc", "def", "g" }</code></td></tr>     * <tr><td><code>","</code></td>     *     <td><code>{ "", "" }</code></td></tr>     * <tr><td><code>",,,b"</code></td>     *     <td><code>{ "", "", "", "b" }</code></td></tr>     * <tr><td><code>"a\,b"</code></td>     *     <td><code>{ "a,b" }</code></td></tr>     * <tr><td><code>"\\\\"</code></td>     *     <td><code>{ "\\" }</code></td></tr>     * <tr><td><code>"ab\\\\"</code></td>     *     <td><code>{ "ab\\" }</code></td></tr>     * <tr><td><code>"\\\n"</code></td>     *     <td><code>{ "\n" }</code></td></tr>     * <tr><td><code>"a\\,bc,d\\,e,,f"</code></td>     *     <td><code>{ "a,bc", "d\", "e", "", "f" }</code></td></tr>     * <tr><td><code>"ab\\"</code></td>     *     <td><code>IllegalArgumentException</code></td></tr>     * <tr><td><code>"a\\bc"</code></td>     *     <td><code>IllegalArgumentException</code></td></tr>     * <tr><td><code>"\"</code></td>     *     <td><code>IllegalArgumentException</code></td></tr>     * <tr><td><code>"\\\"</code></td>     *     <td><code>IllegalArgumentException</code></td></tr>     * </table>     *     * <p>Given the use of java escapes, <code>"ab\\"</code> is     * the three character sequence a, b and backslash, with the     * backslash escaped as <code>\\</code>.  Similarly, <code>"\\\n"</code>     * is the two character string composed of a backslash followed     * by a newline character.     *     * @param csvs Comma-separated values to separate.     * @return Array of values.     * @throws IllegalArgumentException if the input is not well formed.     */    public static String[] csvToArray(String csvs) {        ArrayList list = new ArrayList();        int pos = 0;        StringBuffer sb = new StringBuffer();        while (pos < csvs.length()) {            char c = csvs.charAt(pos++);            if (c == ',') {                list.add(sb.toString());                sb = new StringBuffer();            } else if (c == BACKSLASH_CHAR) {                if (pos >= csvs.length()) {                    String msg = "Illegal end on backslash"                        + " CSVs=|" + csvs + "|";                    throw new IllegalArgumentException(msg);                }                char nextChar = csvs.charAt(pos++);                if (csvEscape(c)) {                    sb.append(nextChar);                } else {                    String msg = "Illegal escape following backslash."                        + " Position=" + pos                        + " CVSs=" + csvs;                    throw new IllegalArgumentException(msg);                }            } else {                sb.append(c);            }        }        list.add(sb.toString());        return Collections.toStringArray(list);    }    /**     * Converts a comma-separated values string to a two-dimensional     * array of strings.  Newlines (<code>\n<\code>) are used to     * separate rows, and each row is in CSV notation, as defined in     * {@link #csvToArray(String)} for more information on the row     * encoding.     *     * <p>Here are some examples:     *     * <p>     * <table cellpadding="3" border="1">     * <tr><td><i>CSV</i></td>     *     <td><i>Array</i></td></tr>     * <tr><td><code>""</code></td>     *     <td><code>{ { "" } }</code></td></tr>     * <tr><td><code>"a"</code></td>     *     <td><code>{ { "a" } }</code></td></tr>     * <tr><td><code>"a,b"</code></td>     *     <td><code>{ { "a", "b" } }</code></td></tr>     * <tr><td><code>"\n"</code></td>     *     <td><code>{ { "" }, { "" } }</code></td></tr>     * <tr><td><code>"\n\n"</code></td>     *     <td><code>{ { "" }, { "" }, { "" } }</code></td></tr>     * <tr><td><code>"\\\n"</code></td>     *     <td><code>{ { "\n" } }</code></td></tr>     * </table>     *     * <p>Any illegal argument for {@link #csvToArray(String)} is also an     * illegal argument for this method.     *     * @param csvs Input comma-separated values.     * @return Array of arrays of strings derived from CSVs.     * @throws IllegalArgumentException If the CVSs are not well formed.     */    public static String[][] csvToArray2D(String csvs) {        ArrayList result = new ArrayList();        int start = 0;        for (int end = start; end < csvs.length(); ++end) {            char c = csvs.charAt(end);            if (c == '\n') {                result.add(csvToArray(csvs.substring(start,end)));                start = end+1;            } else if (c == '\\') {                if (++end >= csvs.length()) {                    String msg = "Premature end on backslash."                        + " csvs=" + csvs;                    throw new IllegalArgumentException(msg);                }                char escapedC = csvs.charAt(end);                if (!csvEscape(escapedC)) {                    String msg = "Illegal escape."                        + " position=" + (end-1)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -