将嵌套ArrayList<ArrayList<Object>>转换为固定大小的数组/列表
Hey there! Let's figure out how to swap your outer ArrayList<ArrayList<Object>> for a fixed-size array or list, while keeping the inner lists mutable for your add operations. Here are two straightforward approaches tailored to your needs:
1. Use a Fixed-Size Array (Most Direct Implementation)
Arrays in Java have a fixed size by nature, which makes them perfect for your requirement. Here's how to set it up:
Declaration
First, declare your array with the inner ArrayList type:
private ArrayList<Object>[] models;
(Note: Java doesn't allow direct creation of generic arrays, so we'll need a cast during initialization.)
Initialization
In your constructor, specify the fixed size (replace 5 with your desired size) and initialize each inner list:
// Initialize the array with your fixed size models = (ArrayList<Object>[]) new ArrayList[5]; // Initialize each inner ArrayList (array elements are null by default) for (int i = 0; i < models.length; i++) { models[i] = new ArrayList<Object>(); }
Adding Elements
You can add elements to the inner lists just like before—only the outer array size is fixed:
// Add to the first inner list models[0].add("Sample string"); // Add to the third inner list models[2].add(456);
Important: You can't resize the array or assign values to indices outside its fixed size (this will throw an ArrayIndexOutOfBoundsException), which aligns perfectly with your goal of a fixed outer container.
2. Use a Fixed-Size List (If You Prefer the List Interface)
If you want to stick with the List interface instead of an array, there are two options depending on how restrictive you want the outer container to be:
Option A: Fixed-Size Mutable Outer List (Allow Modifying Inner List References)
Use Arrays.asList() to create a fixed-size list. You can't add/remove outer elements, but you can replace inner lists and modify their contents:
Declaration
private List<ArrayList<Object>> models;
Initialization
// Create a fixed-size list with your desired size (replace 3 with your size) models = Arrays.asList(new ArrayList[3]); // Initialize each inner ArrayList for (int i = 0; i < models.size(); i++) { models.set(i, new ArrayList<Object>()); }
Usage
// Add elements to inner lists models.get(0).add(new Object()); // Replace an inner list (allowed here) models.set(1, new ArrayList<>(List.of("pre-filled", "items")));
Note: Calling models.add(new ArrayList<Object>()) will throw an UnsupportedOperationException—this is expected, as the list size is fixed.
Option B: Immutable Outer List (Prevent Modifying Inner List References)
If you want to lock down the outer list completely (no adding/removing/replacing inner lists), use Collections.unmodifiableList():
Initialization
// First create a regular ArrayList to set up your inner lists ArrayList<ArrayList<Object>> tempList = new ArrayList<>(); tempList.add(new ArrayList<>()); tempList.add(new ArrayList<>()); tempList.add(new ArrayList<>()); // Convert to an immutable list models = Collections.unmodifiableList(tempList);
Usage
You can still modify inner lists, but you can't change the outer list's structure:
// Works fine models.get(1).add("Mutable inner content"); // Throws UnsupportedOperationException (can't modify outer list) // models.add(new ArrayList<>());
Which Should You Choose?
- Go with the array if you want the simplest, most performant fixed-size container.
- Use
Arrays.asList()if you need theListinterface and want to allow replacing inner lists. - Pick
Collections.unmodifiableList()if you need a fully immutable outer container (only inner lists can change).
内容的提问来源于stack exchange,提问作者John

