• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Initializing Arraylists inside a loop

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You do not need to create the list every time:


You can do

 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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. [=
 
Ranch Hand
Posts: 1325
Android Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic