Java: How to remove objects from an Array dependin

2019-04-21 07:07发布

I have an array of Objects (file list excatly). How to iterate through this array and delete some Objects (in Java) - depending on the condition ?

File[] files = file.listFiles();
for(File f: files) {
   if(someCondition) {
       // remove
   }
}

6条回答
地球回转人心会变
2楼-- · 2019-04-21 07:10

We can't delete elements and resize the arrays in one step/operation. Arrays can't be resized.

Either use a List or (1) flag the elements you want to delete and (2) write the elements you want to keep, to a new array.

Here's a solution if you want to continue with arrays (List is much easier):

private File[] filter(File[] files) {
  boolean[] deleteFlags = new boolean[files.length];
  int deleteCounter = 0;

  // collection
  for (int i = 0; i < files.length; i++) {
    if (deleteConditionIsTrue()) {
       deleteFlags[i] = true;
       deleteCounter++;
    }
  }

  // create result
  File[] result = new File[files.length-deleteCounter];
  int gapCounter = 0;
  for (int i = 0; i < deleteFlags.length; i++) {
    if (deleteFlags[i]) {
      gapCounter++; // skip entry, has been filtered/deleted
    } else {
      result[i-gapCounter] = files[i];
    }          
  }

  return result;
}
查看更多
老娘就宠你
3楼-- · 2019-04-21 07:16

You need to have the index of the object in the array to be able to remove it:

File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
    if(someCondition) {
        files[i] = null;       
    }
}

Note that an array has a fixed length. Removing an element won't shrink the array. If you want this, use a List<File>, iterate through the list using an Iterator, and use the iterator's remove method to remove the current element.

查看更多
你好瞎i
4楼-- · 2019-04-21 07:19

JB Nizet has it exactly right:

  1. You can't "delete" elements from an array

  2. You can set elements to "null". This effectively deletes them (in a C kind of way), but it requires extra logic so you don't accidentally try to reference a null element.

  3. All things being equal, you're probably better off with a List<>, which does allow you to insert and delete elements.

PS: If you know a priori what elements you don't want, the FileFilter idea is an excellent way to keep from getting them in the first place.

查看更多
做个烂人
5楼-- · 2019-04-21 07:24

You may be better off giving FilenameFilter to listFiles and apply condition there. See File documentation http://download.oracle.com/javase/6/docs/api/java/io/File.html

查看更多
贼婆χ
6楼-- · 2019-04-21 07:25

I think the best Java way to tackle your problem is to convert your array into a list and use an iterator which allows you to remove objects:

List<File> files = new ArrayList<File>(Arrays.asList(file.listFiles()));
Iterator<File> iterator = files.iterator();
while(iterator.hasNext()){
    File currentFile = iterator.next();
    if(someCondition){
        iterator.remove();
    }
    // other operations
}

You can even convert it again into an array if necessary -even though handling a list is probably more convenient ...:

File[] filesArray = files.toArray();
查看更多
\"骚年 ilove
7楼-- · 2019-04-21 07:26

I suggest to convert it to list and use LambdaJ filter operation: http://code.google.com/p/lambdaj/wiki/LambdajFeatures. Also filter for lists is available in other libraries, like Guava.

查看更多
登录 后发表回答