How to filter an array in Java?

后端 未结 7 984
小蘑菇
小蘑菇 2020-12-10 02:01

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 Ca         


        
7条回答
  •  爱一瞬间的悲伤
    2020-12-10 02:56

    If you really need a plain array as the result, I think your way is the way to go: you don't know the number of resulting elements before you filter, and you can't construct a new array without knowing the number of elements.

    However, if you don't need thread-safety, consider using ArrayList instead of a Vector. It ought to be somewhat faster. Then use ArrayList's toArray method to get the array.

提交回复
热议问题