What is the difference between an array and an ArrayList in Java?
| Point | Array | ArrayList |
|---|---|---|
| Size | Fixed | Dynamic |
| Types | Primitives and objects | Objects only (use wrappers) |
| Length | arr.length | list.size() |
| Add or remove | Not possible | add(), remove() |
| Generics | No | Yes |
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
int[] arr = new int[3];
List<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
System.out.println(arr.length + " " + list.size());
}
}
Output
3 2
See Java Arrays and Java ArrayList.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |