I have an arraylist of Ellipse2D , the user enter a number is joptionpane.showinputdialog , but he is allowed to enter any number , the problem if he exceed the size of arraylist I have a problem , i search a methode to increase the initial size of arraylist (I found ensurecapacity() but it dosen't work we must call it before we call the methode add?) please help me
2 Answers
An ArrayList resizes as necessary, so you don't need to do anything before calling the add() method. As you add items beyond the initial capacity of the list, the internal array will be expanded. Except for performance issues, the internal mechanisms and the capacity of the ArrayList shouldn't concern you.
The initial capacity is a useful thing to specify only if you know that you will insert a lot of elements to start with, in which case you want to avoid dynamically re-sizing the internal array continuously, but instead it's better to pre-allocate the necessary space in advance.