When I create an array in Java – int array[] and array=new int[some number] –
How can I construct it if I don’t know how many values it will hold so that I have enough space in it?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
For growing arrays, use
ArrayList.If the array should contain primitive types, you can wrap them:
However, when the values change a lot, you keep instantiating and throwing away Integer instances, because it is immutable. It’s not very high performance either.
My solution: create a wrapper yourself, with a public value field.
This is my wrapper, which is also suitable for
TreeMapand the likes, which sort items by their natural order.Now you can do stuff like: