How do I remove objects from an array in Java?

前端 未结 20 1397
Happy的楠姐
Happy的楠姐 2020-11-22 01:20

Given an array of n Objects, let\'s say it is an array of strings, and it has the following values:

foo[0] = \"a\";
foo[1]          


        
相关标签:
20条回答
  • 2020-11-22 01:53

    If it doesn't matter the order of the elements. you can swap between the elements foo[x] and foo[0], then call foo.drop(1).

    foo.drop(n) removes (n) first elements from the array.

    I guess this is the simplest and resource efficient way to do.

    PS: indexOf can be implemented in many ways, this is my version.

    Integer indexOf(String[] arr, String value){
        for(Integer i = 0 ; i < arr.length; i++ )
            if(arr[i] == value)
                return i;         // return the index of the element
        return -1                 // otherwise -1
    }
    
    while (true) {
       Integer i;
       i = indexOf(foo,"a")
       if (i == -1) break;
       foo[i] = foo[0];           // preserve foo[0]
       foo.drop(1);
    }
    
    0 讨论(0)
  • 2020-11-22 01:56

    Initial array

       int[] array = {5,6,51,4,3,2};
    

    if you want remove 51 that is index 2, use following

     for(int i = 2; i < array.length -1; i++){
        array[i] = array[i + 1];
      }
    
    0 讨论(0)
  • 2020-11-22 01:56

    In an array of Strings like

    String name = 'a b c d e a f b d e' // could be like String name = 'aa bb c d e aa f bb d e'

    I build the following class

    class clearname{
    def parts
    def tv
    public def str = ''
    String name
    clearname(String name){
        this.name = name
        this.parts = this.name.split(" ")
        this.tv = this.parts.size()
    }
    public String cleared(){
    
            int i
            int k
            int j=0        
        for(i=0;i<tv;i++){
            for(k=0;k<tv;k++){
                if(this.parts[k] == this.parts[i] && k!=i){
                   this.parts[k] = '';
                    j++
                }
            }
        }
        def str = ''
        for(i=0;i<tv;i++){
            if(this.parts[i]!='')
    
               this.str += this.parts[i].trim()+' '
        } 
        return this.str    
    }}
    
    
    
    return new clearname(name).cleared()
    

    getting this result

    a b c d e f

    hope this code help anyone Regards

    0 讨论(0)
  • 2020-11-22 02:00

    You can always do:

    int i, j;
    for (i = j = 0; j < foo.length; ++j)
      if (!"a".equals(foo[j])) foo[i++] = foo[j];
    foo = Arrays.copyOf(foo, i);
    
    0 讨论(0)
  • 2020-11-22 02:01

    Something about the make a list of it then remove then back to an array strikes me as wrong. Haven't tested, but I think the following will perform better. Yes I'm probably unduly pre-optimizing.

    boolean [] deleteItem = new boolean[arr.length];
    int size=0;
    for(int i=0;i<arr.length;i==){
       if(arr[i].equals("a")){
          deleteItem[i]=true;
       }
       else{
          deleteItem[i]=false;
          size++;
       }
    }
    String[] newArr=new String[size];
    int index=0;
    for(int i=0;i<arr.length;i++){
       if(!deleteItem[i]){
          newArr[index++]=arr[i];
       }
    }
    
    0 讨论(0)
  • 2020-11-22 02:02

    Assign null to the array locations.

    0 讨论(0)
提交回复
热议问题