Sumtinlazy
Sumtinlazy

Reputation: 347

How to create objects dynamically during runtime Java

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.

Upvotes: 0

Views: 44

Answers (1)

uli
uli

Reputation: 671

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

Upvotes: 1

Related Questions