arrayutils.java

来自「Standord Classifier实现了一个基于Java的最大熵分类器。用于」· Java 代码 · 共 57 行

JAVA
57
字号
package edu.stanford.nlp.util;/** * Static utility methods for operating on arrays. * * @author  Huy Nguyen (htnguyen@cs.stanford.edu) */import java.lang.reflect.*;public class ArrayUtils{        /** Should not be instantiated */    protected ArrayUtils(){}        /** Removes the element at the specified index from the array, and returns     *  a new array containing the remaining elements.  If <tt>index</tt> is invalid,     *  returns <tt>array</tt> unchanged.     */    public static double[] removeAt(double[] array, int index) {        if(array==null) return null;        if(index<0 || index>=array.length) return array;                double[] retVal=new double[array.length-1];        for(int i=0; i<array.length; i++) {            if(i<index) retVal[i]=array[i];            else if(i>index) retVal[i-1]=array[i];        }        return retVal;    }             /** Removes the element at the specified index from the array, and returns     *  a new array containing the remaining elements.  If <tt>index</tt> is invalid,     *  returns <tt>array</tt> unchanged.  Uses reflection to determine the type     *  of the array and returns an array of the appropriate type.     */    public static Object[] removeAt(Object[] array, int index) {        if(array==null) return null;        if(index<0 || index>=array.length) return array;                Object[] retVal=(Object[])Array.newInstance(array[0].getClass(),array.length-1);        for(int i=0; i<array.length; i++) {            if(i<index) retVal[i]=array[i];            else if(i>index) retVal[i-1]=array[i];        }        return retVal;    }            /** For internal debugging purposes only */    public static void main(String[] args) {        String[] strings=new String[]{"a","b","c"};        strings=(String[])ArrayUtils.removeAt(strings,2);        for(int i=0; i<strings.length; i++) {            System.err.println(strings[i]);        }    }}

⌨️ 快捷键说明

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