• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamically creating ArrayList inside a iterator

 
satheesh krishnaswamy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • 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,
 
Norm Radder
Ranch Hand
Posts: 728
4
  • Mark post as helpful
  • send pies
  • 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.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or a collection, like a List. So you'd have a list of lists.
 
Adinath Shirsath
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic