0

I'm trying to make a program that is going to need to dynamically create ArrayLists along the lines of this:

for (int i = 0; i < arbitraryInt; i++){
    //code to make 5 new arraylists
}

However I can't seem to find any method that would accomplish this.

Edit: I am trying to make a columnar cipher for my APCS class. This cipher requires me to convert a string of any length into a grid. In this case each arraylist would be a column. So to be more specific my code would look somewhat like this:

String encode = "somethingsomethingyadayadayada";
int x = 0, y = 0;

for (int i = 0; (i*i) < encode.length(); i++){
    y = i;
    x = i-1;
}
for (int i = 0; i < x; i++){
    //make an arraylist
}

Is this better @Kartic? I tried to keep it non specific as to keep my post from becoming a code dump.

5
  • Well, where do you want to put them? What do you want to do with them? Commented Nov 8, 2018 at 2:36
  • what's wrong with using new ArrayList() 5 times, or in a loop? or having List<List<Integer>>? Commented Nov 8, 2018 at 2:38
  • In the final version of what I'm trying to make, the number of arraylists will vary depending certain parameters. Commented Nov 8, 2018 at 2:40
  • @Sumtinlazy what parameters? please complete your question.. "along the lines of this" is not enough Commented Nov 8, 2018 at 2:44
  • I got an answer, updated just for you babe. Commented Nov 8, 2018 at 2:56

1 Answer 1

1

One of the possible solution is to store lists inside list:

List<List<Integer>> listOfLists = new ArrayList<>();
for (int i = 0; i < arbitraryInt; i++){
    listOfLists.add(new ArrayList<>());
}
// get third list
List<Integer> third = listOfLists.get(2);
Sign up to request clarification or add additional context in comments.

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.