为何Java中Array是静态的而ArrayList是动态的?数组大小能否修改及解决方案探讨
Great question—this is a common point of confusion when starting out with Java collections. Let’s break this down clearly.
Java Array: Fixed-size by design
A Java array is a fixed-length block of contiguous memory allocated when you initialize it. When you write int[] arr = new int[5];, the JVM reserves a continuous chunk of memory on the heap that can hold exactly 5 integers. This size is baked into the array's core properties—you can check it with arr.length, and this value never changes after creation.
The reason for this is performance: contiguous memory allows super-fast random access (you can jump directly to any index in O(1) time), but resizing would require finding a new larger contiguous block, copying all elements over, and discarding the old one—operations that are costly and the language doesn’t automate for you.
ArrayList: Dynamic wrapper around arrays
ArrayList is a collection class that wraps a regular Java array (you can think of it as a "smart array"). It maintains an internal array (like transient Object[] elementData under the hood) and handles resizing automatically for you:
- When you add an element and the internal array is full, ArrayList creates a new, larger array (default is 1.5x the original size)
- It copies all elements from the old array to the new one using efficient native methods
- It then replaces the old array with the new one and adds your new element
From your perspective, you never see this behind-the-scenes work—you just keep adding elements, so it feels "dynamic".
You cannot directly change the size of an existing Java array—its length is immutable once created. But you can achieve the effect of resizing with these workarounds:
Manually create a new array and copy elements
This is the low-level approach. For example, to expand an array from 3 to 5 elements:int[] oldArray = {1, 2, 3}; // Create a new array with the desired size int[] newArray = new int[5]; // Copy elements from old to new array (using efficient native method) System.arraycopy(oldArray, 0, newArray, 0, oldArray.length); // Point the original variable to the new array oldArray = newArray;Or use
Arrays.copyOffor a more concise version (it does the same thing under the hood):int[] newArray = Arrays.copyOf(oldArray, 5);Switch to ArrayList (or other collections)
If you need frequent resizing, just use ArrayList—its entire purpose is to handle this complexity for you. You get all the benefits of arrays (fast access) plus dynamic resizing:ArrayList<Integer> dynamicList = new ArrayList<>(); dynamicList.add(1); dynamicList.add(2); // No need to worry about sizing—ArrayList handles expansion automatically dynamicList.add(3); dynamicList.add(4);Use LinkedList for frequent insertions/deletions
If your use case involves lots of adding/removing elements (especially from the middle), LinkedList might be a better fit. It’s based on a linked list structure, so it doesn’t require contiguous memory—resizing is just a matter of adjusting node pointers, no array copying needed. The tradeoff is slower random access compared to arrays/ArrayList.
内容的提问来源于stack exchange,提问作者TechSavy




