Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Initializing Arraylists inside a loop

 
nissan Ca
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

Thankyou.
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello nissan Ca, welcome to the JavaRanch.

Do you have memory limitations?

Could you simple use List list = new ArrayList();

with the code above, you will need to worry about the user input.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
have some nested loops. the outer loops iterates across your collection of arrayLists, and the inner loop iterates across the elements of the current arrayList
 
nissan Ca
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the quick reply, actually im a beginner
and i just want to explore the optimality of the code.
i tried initializing 10 arraylists here

but my desired output should be 10 different arraylists, example, list1,list2,list3,...,list10

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would need to add a reference to each list to a collection of some sort - array, list, set, map. Which you choose depends on what you intend to do with the lists.
 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do not need to create the list every time:


You can do

 
fred rosenberger
lowercase baba
Bartender
Posts: 12149
31
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

nissan Ca,

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.


 
Hebert Coelho
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
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.


He was creating the same list whe a loop was completed.

If he need 10, create all 10 outside the for. [=
 
Muhammad Saifuddin
Ranch Hand
Posts: 1324
Android Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is how you achieve your goal. its just a code example of what the Joanne Neal suggest above.

 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hebert Coelho wrote:If he need 10, create all 10 outside the for. [=

But he doesn't know how many he needs - it depends on the user input.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic