DEV Community

Jostin Soza
Jostin Soza

Posted on

Arrays

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];
Enter fullscreen mode Exit fullscreen mode

We have now declared a variable that holds an array of strings.

To insert values into it:

String[] food = {"Apple", "Carrots", "Pasta", "Pizza"}; 
Enter fullscreen mode Exit fullscreen mode

The same with other data types:

int[] nums = {1,2,3,4};

boolean[] bools = {true, false};

double[] doubleNums = {1.23, 4.52};
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Change an array element

It's the same here; we only have to access the index and assign the new value:

food[0] = "Hamburguer";
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Note. You must use wrapper classes like Integer, Double, etc., in ArrayList because it only stores objects, not primitives.

Resizing

Array (can't resize):

int[] arr = new int[2];
// arr[2] = 100; // ❌ ArrayIndexOutOfBoundsException
Enter fullscreen mode Exit fullscreen mode

ArrayList (dynamic):

ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C"); //Automatically resizes
Enter fullscreen mode Exit fullscreen mode

Common methods in ArrayList:

list.add("Hello");
list.get(0);
list.set(1, "World");
list.remove(0);
list.size();
list.contains("Hello");
Enter fullscreen mode Exit fullscreen mode

When to Use What?

  • Use Arrays when:
  1. The number of elements is known and fixed.
  2. You need to store primitives (e.g., int, char, etc.) for performance.
  3. Memory optimization is crucial.
  • Use ArrayList when:
  1. The number of elements can change dynamically.
  2. You want convenient methods (add, remove, search).
  3. You are working with collections in modern Java.

Top comments (0)