简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM