4

How do we create arraylist dynamically inside a loop?

something like -

for(i=0;i<4;i++)  
{  
List<Integer> arr(i) = new ArrayList<>();  
}         
3
  • Means you need to create 4 arraylist and return an array containing reference to all that arraylists?? Commented Feb 8, 2013 at 7:34
  • yes, i want to create four arraylist Commented Feb 8, 2013 at 7:39
  • I appreciate this question, I have faced serious need for this in competitive programming contests. Commented Dec 18, 2019 at 5:11

7 Answers 7

16

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 :(

Sign up to request clarification or add additional context in comments.

2 Comments

That works fine, Thank you:) And can you tell me how do i access individual arraylist and add elements to it?
@Preethi: See the comment below the loop.
3

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

Comments

3

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(); 
        }
    }
}

Comments

2

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);

}

Comments

2

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.

Comments

1
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.

Comments

-2

I am not sure what you mean, maybe this?

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

2 Comments

You are adding i values ie 1,2,3,4 into arraylist. This is not what OP wants
It's hard to say what author means originally, right? =) Just my guess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.