How can I initialize an array without knowing it size?

前端 未结 4 1089
青春惊慌失措
青春惊慌失措 2021-02-07 01:55

I have a situation, where I have to apply a criteria on an input array and reuturn another array as output which will have smaller size based upon the filtering criteria.

4条回答
  •  滥情空心
    2021-02-07 02:22

    You can't... an array's size is always fixed in Java. Typically instead of using an array, you'd use an implementation of List here - usually ArrayList, but with plenty of other alternatives available.

    You can create an array from the list as a final step, of course - or just change the signature of the method to return a List to start with.

提交回复
热议问题