如何删除数组中某一元素,是java里,不是javascript里

解决方案 »

  1.   

    转换为ArrayList,然后remove(index E)
      

  2.   

    转为LinkedList 再用 remove()也可以!( 如果要经常从表中插入或删除元素)
      

  3.   

    为什么都喜欢偷懒呢?这样的性能不是很好啊.如果这个数组是原始类型的数组,那系统更加消耗大了.
    Integer []aa = {new Integer(1),new Integer(2),new Integer(3)};
    deleteItem(aa,0);
    static boolean deleteItem(Object[] arr ,int itemDel ){
      if (null == arr || arr.length == 0 || itemDel<0 || itemDel>=arr.length)
         return false;
       if (arr.length == 1) {
          arr[0] = null;
          return true;
       }   
       for (int i = itemDel ; i <arr.length-1  ; ++i){
      arr[i] = arr[i+1];
       }
       arr[arr.length-1] = null;
       return true;
    }
      

  4.   

    如果可以的话,尽量使用数组,而不是图方便用Collection.
      

  5.   

    XXX[] oldArray = new XXX[N]; //N is the length of the original array
    XXX[] newArray = new XXX[N - 1];
    System.arraycopy(oldArray, 0, newArray, 0, n); //n is the index of the element you wanna remove
    if (n != N) {
      System.arraycopy(oldArray, n + 1, newArray, n, N - n - 1);
    }
      

  6.   

    两位带双星的编程态度确实值得学习!!
    比较了一下,应该是shine333(enihs) 的速度比laughsmile(海边的星空)的要快些,但是占用的更多的临时资源
      

  7.   

    "转换为ArrayList,然后remove(index E)"
    支持使用ArrayList.
    不要为了一点点性能牺牲可读性。
    要达到最高性能请使用C++或直接使用汇编好了。