• 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

Dynamically creating ArrayList inside a iterator

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

My requirement is this:-

I need to create "n" number of ArrayLists inside for loop dynamically n is based on a field value, which will also change.

Something like this:-
In the first iteration

for(int i=0;i<20;i++)
{
ArrayList firstIteration = new ArrayList();
}

and in the second iteration it should be like this...

for(int i=0;i<20;i++)
{

ArrayList SecondIteration = new ArrayList();
}

Please let me know How do I do this?

Thanks in advance,
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like you should use an array of ArrayLists. Create a new ArrayList and add it as the next element in the array at each iteration thru the for loop.
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or a collection, like a List. So you'd have a list of lists.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
or you can use HashMap also
like

HashMap iterationmap = new HashMap();
for(int i=0;i<20;i++)
{

ArrayList iterationList = new ArrayList();
iterationmap.put("Iteration" + i , iterationList);
}



then you can get lists from map based on iteration no as key
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic