Both Array
and ArrayList
are used to store multiple values in Java — but they work a little differently.
Let’s look at what makes them different, and when to use each one.
Array
-
Fixed size (you can’t change it once created)
-
Can store primitive types like
int
,double
, etc. -
Syntax:
int[] numbers = new int[5];
numbers[0] = 10;
ArrayList Can grow and shrink in size
-
Only works with objects, not primitives (use
Integer
instead ofint
) -
Comes from
java.util.ArrayList
-
Syntax:
import java.util.ArrayList;
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10);
No comments:
Post a Comment