I want to initialize arraylists inside a loop instead of initializing it one by one. is that possible?
the number of arraylists will depend on the user input, so for example the user input a value of 50, i want to simply initialize it inside a loop and will do the initialization of the arraylists.. how will that be possible that 50 different arraylists will be initialized?
Hebert Coelho wrote:You do not need to create the list every time
Doesn't that kind of depend on what is really needed? Maybe the OP really needs 10 arrayLists.
tell us what you really are trying to do. Do you need to have ten (or whatever) lists of integers, or can you use one list and clear/reset it every time?
nissan Ca wrote:my desired output should be 10 different arraylists, example, list1,list2,list3,...,list10
I'm not sure if this is what you are trying to do, but you cannot dynamically name variables. You would either need to explicitly declare all of your list1, list2, etc, but then you don't know how many you will need, so that's probably not idea.
If you need to keep/maintain many lists, you need a collection of lists. For example, if you were keeping track of lottery ticket, you'd need a list of ticket, and each ticket would have a list of integers picked for that ticket.
You would reference the collection of tickets, and then either iterate through it to see each one, or use various methods to access a specific element.
It's difficult to guide you without knowing exactly what you are trying to accomplish.