What Is Array in Data Structure Example?


Larry Thompson

What Is Array in Data Structure Example?

In computer science, an array is a data structure that stores a fixed-size sequential collection of elements of the same type. Each element in an array is identified by its index, which is a non-negative integer value. Arrays can be used to store data of any type, including numbers, characters, strings, and even other arrays.

Array Declaration

To declare an array in most programming languages, you would need to specify the type of elements it will hold and the size of the array. For example:

int[] numbers = new int[5];

This declares an integer array named “numbers” with a size of 5. The index of the first element is 0, and the index of the last element is 4.

Accessing Array Elements

You can access individual elements in an array using their respective indices. For example:

int secondNumber = numbers[1];

This assigns the value at index 1 in the “numbers” array to the variable “secondNumber”. Remember that arrays are zero-indexed, so accessing index 1 gives you the second element.

Modifying Array Elements

You can modify the value of an element in an array by assigning a new value to it. For example:

numbers[3] = 42;

This changes the value at index 3 in the “numbers” array to 42.

Array Example: Storing Names

Let’s consider an example where we want to store a list of names using an array. Here’s how we can declare and initialize the array:

String[] names = {"Alice", "Bob", "Charlie", "Dave"};

We have declared a string array named “names” and initialized it with four names. The first name, “Alice”, is at index 0, the second name, “Bob”, is at index 1, and so on.

Iterating Over Array Elements

To access all elements in an array sequentially, you can use a loop. Here’s an example using a for loop:

for (int i = 0; i < names.length; i++) {

This loop iterates from 0 to the length of the "names" array minus one and prints each name on a new line.


Arrays are an essential data structure in programming that allow us to store and manipulate collections of elements efficiently. By understanding how to declare, access, modify, and iterate over array elements, you can leverage their power to solve various problems in your programs.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy