1

The user will input a value between 1 and 5. Base on the input value, I would like to create multiple arraylist with different names.

Example 1 (when input = 3) :

ArrayList<String> Group0 = new ArrayList<>();
ArrayList<String> Group1 = new ArrayList<>();
ArrayList<String> Group2 = new ArrayList<>();

Example 2 (when input = 5) :

ArrayList<String> Group0 = new ArrayList<>();
ArrayList<String> Group1 = new ArrayList<>();
ArrayList<String> Group2 = new ArrayList<>();
ArrayList<String> Group3 = new ArrayList<>();
ArrayList<String> Group4 = new ArrayList<>();

I apologise beforehand if this question seem trivial. I am relatively new to coding and thus would be very grateful to anyone who might know of an available method/command to get the above results. Thankyou!


I have tried to do it this way but i cant seem to use "label" when initialising arraylist. The error states "variable label is already defined in method main(String[]).

public static void main(String[] args) {

    int userInput = 3;
    int count = 0;

    while(count < (userInput)) {
        String label = "Group" + Integer.toString(count);
        ArrayList<String> label = new ArrayList<>();
        count++;
    }
}
Mohamed Syafiq
  • 25
  • 1
  • 1
  • 5
  • Use a loop and store these lists in another list? What exactly are you trying to accomplish? – beatngu13 May 28 '17 at 10:00
  • maybe this will help (create multi dimension arraylist).https://stackoverflow.com/questions/4401850/how-to-create-a-multidimensional-arraylist-in-java – OferP May 28 '17 at 10:01
  • @beatngu13 I am trying to create an arraylist that correspond to a certain value that is not hardcoded. That value ranges from 1 to 5. So, if the value is 2, i would like to automatically create group0 and group1. if the value is 4, i would like to automatically create group0, group1, group2. as u can see above, the names of the arraylist changes too when more arraylist are created. – Mohamed Syafiq May 28 '17 at 10:10
  • Possible duplicate of [How to create a Multidimensional ArrayList in Java?](https://stackoverflow.com/questions/4401850/how-to-create-a-multidimensional-arraylist-in-java) – Anton Malmygin May 28 '17 at 10:39
  • @OferP Thankyou so much for the link! Yes it did help!! After reading that article, I have some ideas of how to code what Im looking for... Thankyou again! – Mohamed Syafiq May 28 '17 at 10:43

3 Answers3

1

You can use array of ArrayList for your requirement with given size by user input.

ArrayList<String> lists[]=new ArrayList[input];

for(int i=0;i<input;i++){
   lists[i]=new ArrayList<>();
}

Use lists[0] to access your first list and lists[1] for second and so on.

ArrayList is an option that provide dynamic arrays in Java, but that is not your requirement so use array also because ArrayList is slower than array.


Furthermore if your requirement is more specifically with name Group1, Group2..., You can use HashMap for the same.

HashMap<String,ArrayList<String>> maps=new HashMap<String, ArrayList<String>>();

for (int i=0;i<input;i++)
     maps.put("Group"+String.valueOf(i),new ArrayList<String>());

When your want to retrieve arraylist from map use

maps.get("Group0");
Abhishek Aryan
  • 19,936
  • 8
  • 46
  • 65
0

create a List of list and then with a for loop you can add your list to your general list. for example :

List<List<String>> generalList = new ArrayList<>();
for(int i=0;i<input;i++)
{
 ArrayList<String> myGroup = new ArrayList<>();
 generalList.add(myGroup);
}
Reza.Abedini
  • 2,227
  • 2
  • 16
  • 18
  • Okay, im giving this a try now... thankyou so much for such a prompt reply! – Mohamed Syafiq May 28 '17 at 10:13
  • is it possible to have the name "myGroup" to automatically change after every for loop? maybe for example, when i=0, ArrayList is named "myGroup". when i=1, ArrayList is named "myGroup1". when i=2, ArrayList is named "myGroup2". etc etc – Mohamed Syafiq May 28 '17 at 10:16
  • no it is not a common way.why you want something like this? you can create a class for your need, create a class that contains a String for name and a List of string – Reza.Abedini May 28 '17 at 10:26
  • if you want to retrieve specific index in your list you can write this code : generalList.get(yourGroupIndex) for example you should write generalList.get(5) instead of "myGroup5" – Reza.Abedini May 28 '17 at 10:29
  • Ah..... i get what your trying to say. Thankyou so much!!! I'll have to re-think of how i want to go about it and use .get() Thankyou so much! – Mohamed Syafiq May 28 '17 at 10:45
  • your welcome buddy, let me know if it solved your problem. – Reza.Abedini May 28 '17 at 10:47
  • I would like to apologise for the "noob" questions. Im not a programmer by education / profession. Just picking it up to learn and hopefully have it as a hobby... so pardon me for asking probably "stupid" questions.. :) – Mohamed Syafiq May 28 '17 at 10:47
  • It's fine, it is not a stupid question and i will happy if i could help you :) – Reza.Abedini May 28 '17 at 10:54
0

I think your question can be contrate into one simple thing: to create N ArrayList by N input by the user entered.

So you need a List object to store the N ArrayList objects;

So my code looks like :

import java.util.List;

import com.google.common.collect.Lists;

public class MultipleTest {

    public static void main(String[] args) {
        List<List<String>> groupList = Lists.newArrayList();
        int input = 5;
        int N = input;
        for (int i = 0; i < N; i++) {
            List<String> group_i = Lists.newArrayList();
            groupList.add(group_i);
        }
        System.out.println(groupList);
    }
}
Vincent Jia
  • 592
  • 6
  • 14