Skip to content

This repository contains my solutions to various problems and my practice implementations of data structures and algorithms in Java.

Notifications You must be signed in to change notification settings

ElbazAhmed/Data-Structures-and-Algorithms

Repository files navigation

Arrays - ArrayList

Informally, an array is a list of elements. These elements can be of any type: numbers, words, objects, or even other arrays. Each element in an array is accessed by its index, which starts from 0. Arrays are typically enclosed in square brackets with elements separated by commas, like this: [1, 2, 3]. In this example, the elements of the array are 1, 2, and 3.

Static Size Property

Arrays in many programming languages, including Java, have a fixed size. This means that once an array is created, its size cannot be changed. This static size property can be a limitation when the number of elements is not known in advance or if the size of the array needs to change dynamically.

int[] numbers = new int[3]; // An array of fixed size 3
numbers[0] = 1;
numbers[1] = 2;
numbers[2] = 3;

Solution with ArrayList

To overcome the limitation of fixed size arrays, Java provides the ArrayList class, which is part of the java.util package. ArrayList is a resizable array implementation of the List interface. It can grow and shrink as needed, providing more flexibility.

import java.util.ArrayList;
ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);

Memory Allocation of an Array

When an array is created, a contiguous block of memory is allocated to hold its elements. The size of this memory block is determined by the number of elements and the type of each element. For example, an array of integers (int[]) in Java will allocate memory for the integer values based on their data type size.

int[] array = new int[5]; // Allocates memory for 5 integers

Each element in the array is stored in a contiguous memory location, which allows for efficient access using the index. However, this also means that resizing the array (increasing or decreasing its size) is not straightforward and typically requires creating a new array and copying the elements, which is why using an ArrayList can be more convenient for dynamic data structures.

Helpful Resources

An Overview of Arrays and Memory (Data Structures & Algorithms #2)
What is an Array? - Processing Tutorial
Declare, Initialize, and Use an Array - Processing Tutorial

Releases

No releases published

Packages

No packages published

Languages