title: Java - delete some files from File[] array
body: <p>I have array File[] polFiles. Some files can be deleted by another function and I write function to clean remote files from an array. For example, if there was P0, P1, P2, P3, P4 in File [] polFiles, and were deleted by P1 and P2, now polFiles shall consist of P0, P2 and P4. How to make it? I wrote simple code but it throws any way exceptions and errors.</p>
  
  <pre><code>int delcount = 0;
  for (File file : files) {
      if (!file.exists()) {
          delcount++;
      }
  }
  File[] newfiles = new File[files.length-delcount];
  int fcount = 0;
  for (int i = 0; i &lt; newfiles.length; i++) {
      if (!files[i].exists()) {
          fcount++;
          for (int j = i; j &lt; files.length-fcount-1; j++) {
              newfiles[j] = files[j+fcount];
          }
      } else {
          newfiles[i] = files[i+fcount];
      }
  }
  System.arraycopy(newfiles, 0, files, 0, newfiles.length);
  for (int i = newfiles.length; i &lt; files.length; i++) {
      files[i] = null;
  }
  </code></pre>
  
  <p>Where is a mistake? This code throws Null exception and don't correctly work. It deletes only first file from array</p>