ArrayList索引越界异常排查求助:多维与普通ArrayList数据导入报错
Hey there, let's break down this IndexOutOfBoundsException you're hitting—super common when working with ArrayLists, especially nested ones! The error tells you exactly what's going wrong: you tried to access index 1 in an ArrayList that only has 1 element (remember, ArrayList indexes start at 0, so the only valid index here is 0). Let's walk through the most likely causes tied to your scenario of importing integers into a multidimensional ArrayList and a regular ArrayList, plus fixes for each:
1. Multidimensional ArrayList inner lists aren't properly initialized
If you created the outer ArrayList but didn't set up enough inner sub-lists before trying to access them, you'll hit this error. For example:
// Wrong: Outer list only has 1 inner list, but you try to access index 1 ArrayList<ArrayList<Integer>> multiList = new ArrayList<>(); multiList.add(new ArrayList<>()); // Outer list size = 1, only index 0 exists multiList.get(1).add(5); // Boom—this throws your exact exception
Fix it:
Initialize all required inner lists first, or check the outer list's size before accessing inner indexes:
// Initialize 2 inner lists upfront if you need them ArrayList<ArrayList<Integer>> multiList = new ArrayList<>(); for (int i = 0; i < 2; i++) { multiList.add(new ArrayList<>()); } // Now you can safely access multiList.get(0) and multiList.get(1)
2. Using set() instead of add() when populating the regular ArrayList
A common mistake: set() requires the target index to already exist in the ArrayList (i.e., the list's size must be larger than the index). If you try to use set() on an empty or undersized list, you'll get this exception. Example:
int[] nums = {10, 20}; ArrayList<Integer> normalList = new ArrayList<>(); normalList.set(1, nums[1]); // Wrong—list size is 0, index 1 doesn't exist
Fix it:
Use add() to append elements (it automatically expands the list), or initialize the list with enough placeholder values if you need to use set():
int[] nums = {10, 20}; ArrayList<Integer> normalList = new ArrayList<>(); // Safe way: add elements one by one for (int num : nums) { normalList.add(num); }
3. Mixed-up indexes in nested loops
If you're using nested loops to populate the multidimensional ArrayList, it's easy to mix up outer/inner loop indexes and try to access a sub-list that hasn't been created yet. For example:
int[][] numArray = {{1,2}, {3,4}}; ArrayList<ArrayList<Integer>> multiList = new ArrayList<>(); for (int i = 0; i < numArray.length; i++) { multiList.add(new ArrayList<>()); for (int j = 0; j < numArray[i].length; j++) { // Wrong: Using j to access outer list indexes, which only has i+1 elements so far multiList.get(j).add(numArray[i][j]); } }
Fix it:
Grab the current inner sub-list first and work directly with it to avoid index confusion:
int[][] numArray = {{1,2}, {3,4}}; ArrayList<ArrayList<Integer>> multiList = new ArrayList<>(); for (int i = 0; i < numArray.length; i++) { ArrayList<Integer> currentSubList = new ArrayList<>(); multiList.add(currentSubList); for (int num : numArray[i]) { currentSubList.add(num); // No index guesswork needed } }
Quick Debug Tip
Add a print statement right before the line that throws the error (line 303 in your code) to see exactly what's happening:
// Insert this right before your get() call System.out.println("List size: " + yourTargetList.size() + ", Index being used: " + yourIndex);
This will immediately show you why the index is out of bounds—you'll likely see the size is smaller than the index you're trying to access.
内容的提问来源于stack exchange,提问作者Cailean Finn

