The next topic we're going to discuss is the arrays in Java.
Firstly, we need to know what an array is. An array is a container object that holds a fixed number of values of a single type. Arrays are a fundamental data structure used to store collections of data elements of the same type.
Characteristics of Java arrays:
Fixed Size: The size of an array is determined at the time of its creation and cannot be changed later.
Homogeneous Elements: All elements within an array must be of the same data type. This can be primitive types (like int
, double
, boolean
) or reference types (like String
, Object
, or custom class objects).
Indexed: Array elements are accessed by their numerical index, starting from 0 for the first element.
Objects: In Java (and other languages like javascript
), arrays are objects, meaning they are reference types and are allocated on the heap.
To declare an array:
Define the variable type with square brackets:
String[] food; // Its the most recommended
//or
String food[]; //Also valid
///====CREATING AN ARRAY====
numbers = new int[5]; // Creates an array with 5 elements (default values: 0)
///You can also declare and initialize in one line:
int[] numbers = new int[5];
We have now declared a variable that holds an array of strings.
To insert values into it:
String[] food = {"Apple", "Carrots", "Pasta", "Pizza"};
The same with other data types:
int[] nums = {1,2,3,4};
boolean[] bools = {true, false};
double[] doubleNums = {1.23, 4.52};
Access the elements of an array
You can access an array element by referring to the index number:
System.out.println(food[0]); //Output: Apple
Change an array element
It's the same here; we only have to access the index and assign the new value:
food[0] = "Hamburguer";
Array Length
To find out how many elements an array has, use the length
property:
String[] names = {"Juan", "Mario"};
System.out.println(names.length); //Ouputs: 2
Differences between Array
and ArrayList
In Java, both are used to store collections of elements, but they have differences in how they work, and they're used for.
Feature | Array |
ArrayList |
---|---|---|
Size | Fixed after creation | Dynamic (can grow/shrink) |
Type | Can store primitives or objects | Only stores objects (use wrapper types) |
Syntax | More basic | Easier and more flexible to use |
Performance | Slightly faster (less overhead) | Slightly slower (more functionality) |
Part of | Core Java (language-level feature) | Part of Java Collections Framework |
Utility methods | Limited | Rich set of methods (add, remove, etc.) |
Example: Array vs ArrayList
➤ Array
int[] numbers = new int[3];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
System.out.println(numbers[1]) // 20
➤ ArrayList
import java.util.ArrayList;
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
System.out.println(numbers.get(1)); // 20
Note. You must use wrapper classes like
Integer
,Double
, etc., inArrayList
because it only stores objects, not primitives.
Resizing
Array (can't resize):
int[] arr = new int[2];
// arr[2] = 100; // ❌ ArrayIndexOutOfBoundsException
ArrayList (dynamic):
ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C"); //Automatically resizes
Common methods in ArrayList:
list.add("Hello");
list.get(0);
list.set(1, "World");
list.remove(0);
list.size();
list.contains("Hello");
When to Use What?
- Use Arrays when:
- The number of elements is known and fixed.
- You need to store primitives (e.g.,
int
,char
, etc.) for performance. - Memory optimization is crucial.
- Use ArrayList when:
- The number of elements can change dynamically.
- You want convenient methods (add, remove, search).
- You are working with collections in modern Java.
Top comments (0)