如题,去掉后要存到一个新的数组里

解决方案 »

  1.   


    public class RemoveArrayElement {

    public static String[] remove(String[] src, int index) {
    if (src==null || index<0 || index>=src.length) return null;
    String[] r = new String[src.length-1];
    for (int i=0; i<index; i++) r[i] = src[i];
    for (int i=index+1; i<src.length; i++) r[i-1] = src[i];
    return r;
    }
                         
    public static void main(String[] args) {
    String[] ss = {"1","2","3"};
    System.out.println(Arrays.toString(remove(ss, 0)));
    }}
      

  2.   


       public static String[] remove(String[] src, int index) {
          if(src == null || index < 0 || index >= src.length)
             return null;
          
          String[] newStr = new String[src.length - 1];
          System.arraycopy(src, 0, newStr, 0, index);
          System.arraycopy(src, index + 1, newStr, index, src.length - index - 1);      return newStr;
      }