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++;
    }
}
5
  • Use a loop and store these lists in another list? What exactly are you trying to accomplish? Commented May 28, 2017 at 10:00
  • maybe this will help (create multi dimension arraylist).stackoverflow.com/questions/4401850/… Commented May 28, 2017 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. Commented May 28, 2017 at 10:10
  • Possible duplicate of How to create a Multidimensional ArrayList in Java? Commented May 28, 2017 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! Commented May 28, 2017 at 10:43

3 Answers 3

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");
Sign up to request clarification or add additional context in comments.

Comments

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

8 Comments

Okay, im giving this a try now... thankyou so much for such a prompt reply!
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
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
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"
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!
|
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);
    }
}

Comments

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.