Junior
How does an array differ from a list?
sobes.tech AI
Answer from AI
Arrays have a fixed size determined at creation. Lists can change their size dynamically.
| Characteristic | Array | List |
|---|---|---|
| Size | Fixed | Dynamic |
| Element type | Can be primitive or reference | Usually stores objects (reference types) |
| Access performance | Fast access by index O(1) |
Fast access by index O(1) |
| Insertion/deletion performance | Costly operations O(n) |
Depends on implementation (e.g., ArrayList - O(n) in the middle, LinkedList - O(1) at beginning/end) |
Example of creating an array:
// Creating an array of 5 integers
int[] intArray = new int[5];
Example of creating a list (e.g., ArrayList):
// Creating a list of integers
ArrayList<Integer> intList = new ArrayList<>();