Hi,
How to Definie an array that I do not know what size it will be?
this is the array:
Shape shapes[] = new Shape[];
i dont know how many shapes will be entered,
what am i suppose to write in the second brackets?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi,
How to Definie an array that I do not know what size it will be?
this is the array:
Shape shapes[] = new Shape[];
i dont know how many shapes will be entered,
what am i suppose to write in the second brackets?
In the second set of brackets you put the number of items to be contained in the array. If you need to add more items, you will need to reallocate the array and copy the elements. You can use the Arrays.copyOf() method to do this. However, I recommend using an instance of ArrayList() in lieu of an array since it does all of this book keeping for you.
Regards,
Jim
Yehonatan (October 2nd, 2018)