title: How to filter an array in Java?
body: <p>How can I filter an array in Java?</p>
  
  <p>I have an array of objects, for example cars:</p>
  
  <p>Class:</p>
  
  <pre><code>public class Car{
      public int doors;
      public Car(int d){
          this.doors = d;
      }
  }
  </code></pre>
  
  <p>Use:</p>
  
  <pre><code>Car [] cars = new Cars[4];
  cars[0] = new Car(3);
  cars[1] = new Car(2);
  cars[2] = new Car(4);
  cars[3] = new Car(6);
  </code></pre>
  
  <p>Now I want to filter the array of cars, keeping only 4 doors and more:</p>
  
  <pre><code>for(int i = 0; i&lt;cars.length; i++){
      if(cars[i].doors &gt; 4)
           //add cars[i] to a new array
      }
  }
  </code></pre>
  
  <p>How should I do this?</p>
  
  <p>Before I did it with a Vector:</p>
  
  <pre><code>Vector subset = new Vector();
  for(int i = 0; i&lt;cars.length; i++){
      if(cars[i].doors &gt; 4)
           //add cars[i] to a new array
          subset.addElement(cars[i]);
      }
  }
  </code></pre>
  
  <p>And then I would make a new array with the size of the Vector. Then I would loop over the vector again and fill the new array. I know this is a very large procedure for something simple. </p>
  
  <p><em>I'm using J2ME.</em></p>