How to filter an array in Java?
问题 How can I filter an array in Java? I have an array of objects, for example cars: Class: public class Car{ public int doors; public Car(int d){ this.doors = d; } } Use: 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); Now I want to filter the array of cars, keeping only 4 doors and more: for(int i = 0; i<cars.length; i++){ if(cars[i].doors > 4) //add cars[i] to a new array } } How should I do this? Before I did it with a Vector: