Skip to content

Create differents arraylist in one line

I am a beginner in Java and I was working on ArrayList. On a project I have a list of students . I want to create an Array list for each of these students. It’s easy when you know exactly the number of students. But in that case we are not able to know the number exactly.

I have made a loop that is taking one by one all the parameters of each students. I was wondering if maybe we could create an arrayList “automatically” by just changing the name of it ? Like : (ive called x a list of word for exemple).

for(int i =o;i<x.length;i++){
   ArrayList<Matiere>x[i]  = new ArrayList<>();

The loop will be executed the number of times there are students and it will create an arraylist for each without me implementing them one by one. But it’s not working. Do you have any ideas ?

Thanks !



If you want create an ArrayList for each students.

    Map<String, List<String>> map = new HashMap<>();

    for (int i = 0; i < count; i++) {
        map.put("name" + i, new ArrayList<>());
User contributions licensed under: CC BY-SA
9 People found this is helpful