wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Initializing Arraylists inside a loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Initializing Arraylists inside a loop" Watch "Initializing Arraylists inside a loop" New topic
Author

Initializing Arraylists inside a loop

nissan Ca
Greenhorn

Joined: Nov 04, 2011
Posts: 2
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

Joined: Jul 14, 2010
Posts: 754

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.


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11252
    
  16

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


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
nissan Ca
Greenhorn

Joined: Nov 04, 2011
Posts: 2
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

Joined: Aug 05, 2005
Posts: 3482
    
  13
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.


Joanne
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

You do not need to create the list every time:


You can do

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11252
    
  16

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

Joined: Jul 14, 2010
Posts: 754

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

Joined: Dec 06, 2005
Posts: 1321

this is how you achieve your goal. its just a code example of what the Joanne Neal suggest above.



Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3482
    
  13
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.
 
 
subject: Initializing Arraylists inside a loop