Day 1 of DSA: Arrays Fundamentals

Published: (December 31, 2025 at 09:07 PM EST)
5 min read
Source: Dev.to

Source: Dev.to

Introduction

I chose arrays as the starting point for my DSA journey. Although I am not a complete beginner—I have learned Java and basic DSA earlier—I felt arrays are a good concept to revisit and strengthen before moving to more advanced topics.

However, I would not recommend absolute beginners start directly with arrays. Beginners should first be comfortable with basic programming concepts such as variables, data types, and loops. Once those fundamentals are clear, arrays become much easier to understand.

Before learning DSA, it is important to choose a programming language. I have chosen Java as my primary language, and in this blog I will cover DSA concepts specifically from a Java perspective, with brief comparisons to Python where necessary.

A quick thanks to the instructor of this YouTube tutorial—it helped me understand the concepts better.

Topics Covered in This Blog

  • Why do we need Arrays?
  • What is an Array?
  • Arrays in Java and Python
  • Syntax of an Array in Java
  • Rules of Arrays in Java
  • Declaration and Initialization
  • Example of Creating an Array
  • Indexing in Arrays
  • Length of an Array
  • Default Values in Arrays
  • String Arrays and Memory
  • Primitive Types vs Objects
  • Mutability of Arrays

Why Do We Need Arrays?

Arrays are used to store a collection of related values under a single name, which is usually called a reference variable.

For example, if we want to store the names of our friends, instead of creating multiple variables for each name, we can store all the names in a single array called friendsNames. This makes the code more organized, scalable, and easier to work with.

When I picture an array, I imagine a row of empty boxes on a shelf. Each box has a fixed position number, and I can drop one related item per box. That mental model makes it easier to write loops later because I know each index follows a predictable pattern.

What Is an Array?

A commonly used definition of an array is:

An array is a collection of elements of the same data type stored in contiguous memory locations.

This definition is completely accurate for low‑level languages such as C and C++. However, the internal implementation of arrays can differ across programming languages.

“Contiguous” simply means the slots are side by side in memory. The benefit is fast random access: jumping to index i is quick because the program can calculate the exact memory address. The trade‑off is that resizing usually means creating a brand‑new array and copying items over.

Arrays in Java

  • In Java, an array can store only one data type, which makes arrays homogeneous.
  • Arrays in Java are objects, and they are created in the heap memory.
  • For primitive data types (int, double, etc.), the values are stored contiguously within the array object.
  • For reference types (String, Object, etc.), the array stores contiguous references, while the actual objects may be located at different places in the heap.

I like to remember that String[] names really holds references. Changing names[0] = "Bob" only swaps which String object that slot points to; it does not move or copy the underlying String objects elsewhere.

Arrays in Python

  • Python does not provide traditional arrays by default.
  • What is commonly used in Python is a list, which can store different data types.
  • A Python list stores references to objects, not the raw values themselves.

Because Python lists are dynamic, appending feels effortless. The runtime quietly reallocates and copies when needed. That is friendly for beginners but also hides the cost of growing a list compared to a fixed‑size Java array.

Syntax of an Array in Java

datatype[] arrayName = new datatype[size];

Explanation

SymbolMeaning
datatypeSpecifies the type of elements the array can store
[]Indicates that the variable is of array type
arrayNameReference variable that points to the array object
newUsed to create an array object in the heap
datatype[size]Specifies the data type and the number of elements the array can hold

When I first wrote this, the position of [] confused me. datatype[] arrayName and datatype arrayName[] both compile in Java, but I stick to the first style because it reads as “arrayName is an array of datatype.” The size is fixed at creation time.

Rules of Arrays in Java

  • The reference variable name must follow the same naming conventions as normal variables.
  • All elements in an array must be of the same data type.
  • The size of an array is fixed once it is created and cannot be changed.

If I think I will need a changing size, I start with ArrayList instead. Sticking to plain arrays is great practice for learning indexing and for interview‑style questions where fixed‑size storage is expected.

Declaration and Initialization

datatype[] arr;               // declaration
arr = new datatype[size];      // initialization
  • During declaration, a reference variable is created.
  • During initialization, the array object is created in the heap.
  • Declaration is checked at compile time.
  • Memory allocation for the array happens at runtime.

An easy mistake: calling arr[0] before arr is initialized triggers a NullPointerException because the reference points to nothing. I keep reminding myself that declaration alone does not create the boxes—initialization does.

Example of Creating an Array

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

This creates and initializes an integer array containing six elements.

Whenever I declare with curly braces, I read the va… (the original text ends abruptly here).

Indexing in Arrays

Indexing is used to access elements of an array.

  • Array indexing in Java starts from 0.
  • The first element is stored at index 0.

Example

int[] num = {2, 7, 8};
  • num[0]2

  • num[1]7

  • num[2]8

  • Length of the array = 3

  • Last index = length - 1 = 2

Many beginners confuse the length of an array with the last index, but they are not the same.

If we try to access an index greater than the last index, Java throws an exception called ArrayIndexOutOfBoundsException.

I still pause before writing loop bounds:

for (int i = 0; i

Additional Resources

  • GeeksforGeeks introduction to arrays
  • W3Schools Java arrays overview
  • freeCodeCamp guide on array basics

If you made it here, thanks for learning with me. I’m still treating this like a lab notebook: simple drawings, small code snippets, and honest mistakes I catch along the way. If you spot gaps or have a favorite beginner‑friendly resource, let me know so I can keep improving this journal.

Back to Blog

Related posts

Read more »