1

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

1
  • please post some sample code you tried.. Commented Feb 9, 2014 at 14:34

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

To add elements to an ArrayList, simply call add() as many times as you need. This will automatically resize the list to accommodate the new elements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.