Preethi
Preethi

Reputation: 120

Dynamically creating ArrayList inside a loop

How do we create arraylist dynamically inside a loop?

something like -

for(i=0;i<4;i++)  
{  
List<Integer> arr(i) = new ArrayList<>();  
}         

Upvotes: 4

Views: 45722

Answers (7)

2rahulsk
2rahulsk

Reputation: 508

Create an ArrayList having an ArrayList as it's elements.

ArrayList<ArrayList<Integer>> mainArrayList = 
new ArrayList<ArrayList<Integer>>();

you can add elements into mainArrayList by:

ArrayList<Integer> subArrayList;

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

    subArrayList = new ArrayList<Integer>();
    subArrayList.add(1); // I'm adding a random value to subArrayList
    mainArrayList.add(subArrayList);

}

Now, the mainArrayList will have the 4 arrayLists that we added and we can access each element(which are ArrayLists) using for loop.

Upvotes: 2

Anwarul
Anwarul

Reputation: 43

Maybe you want something like this. This will be creating a number of "List" as much as you want. In this case, I am creating two Lists:

import java.util.LinkedList;
import java.util.List;

public class NumberOfList {
    public static void main (String [] args){
        List<Integer> list[];
        list = new LinkedList[2];
        for(int x=0; x<2; x++){ 
            list[x]= new LinkedList(); 
        }
    }
}

Upvotes: 3

Mukul Kashiv
Mukul Kashiv

Reputation: 41

Creating a list of Arraylist:

import java.io.*;
import java.util.Scanner;
public class Solution {

  public static void main(String[] args) {

    int i=0;

    Scanner obj=new Scanner(System.in);

    List<List<Integer>> lists = new ArrayList<List<Integer>>();
    System.out.println("Enter the number of lists");
    int n=obj.nextInt();

    while (i<n) {
      List<Integer> list = new ArrayList<Integer>();
      System.out.println("Enter the number of integers you want to enter in an ArrayList");

      int d=obj.nextInt();
      for(int j=0;j<d;j++){
        list.add(obj.nextInt());
      }
      lists.add(list);
      System.out.println("List "+i+ "is created");
      System.out.println(lists.get(i));
      System.out.println("");
      i++;
    } //end of while

  } //end of main
} //end of class

Upvotes: 3

Nightswatch
Nightswatch

Reputation: 143

You can try this.

List<List<Integer>> dataList = new ArrayList<List<Integer>>();

for(int i = 1; i <= 4; i++)
{
 List<Integer> tempList = new ArrayList<Integer>();
 dataList.add(tempList);
}

For adding data

for(int i = 1; i <= 4; i++)
{
int value = 5+i;
  dataList.get(i - 1).add(value);

}

Upvotes: 2

xvorsx
xvorsx

Reputation: 2442

I am not sure what you mean, maybe this?

List<Integer> arr = new ArrayList<Integer>();  
for(i=0;i<4;i++)  
{  
    arr.add(i);  
}     

Upvotes: -2

Milan
Milan

Reputation: 345

List<List<Integer>> dataList = new ArrayList<List<Integer>>();
for(i=0;i<4;i++)  
{  
 List<Integer> arr = new ArrayList<>();  

 dataList .add(arr );
}

This might help you. If not, please clarify the scenario.

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500555

It sounds like what you actually want is a list of lists:

List<List<Integer>> lists = new ArrayList<List<Integer>>();
for (int i = 0; i < 4; i++) {
    List<Integer> list = new ArrayList<>();
    lists.add(list);
    // Use the list further...
}

// Now you can use lists.get(0) etc to get at each list

EDIT: Array example removed, as of course arrays of generic types are broken in Java :(

Upvotes: 16

Related Questions