📄 arrayutils.java
字号:
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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -