Is there a way to use ArrayList() method to add strings to an array from user input? I have a program that prompts the user to enter a certain input from a JTextBox and I would like to store this input into an array.
I am student, therefore hints and explanations would be preferred over simple answers.
Thanks in advance!
ArrayListis a class, not a method. If you want to put the strings in an array, you need to keep track of1) The size of the array, or a reasonable upper bound on how many values can be entered.
2) The current index
and you can just put an entered string into the array.
This has the disadvantage that you need to know the size of the array up front.
If you don’t, then you can use a
Listimplementation.List<String> strings = new ArrayList<String>();and then just use the
addmethod to add Strings.To get an array back, you can do
strings.toArray()This obviously has the disadvantage that you only get the array at the end, so it’s not acceptable if you need to use an array for the entire process.