TeachingBee

What Are Different Types Of Arrays?

types of arrays

An array is a data structure that stores a collection of elements of the same data type. The elements are stored contiguously in memory and can be accessed using indices. Arrays allow storing multiple values in an organised manner.

In this article we will dive into different types of arrays and its implementation in various programming languages. So let’s dive in.

Different Types of Arrays

Here are the main different types of arrays:

OneDimensional array – A simple linear array with a single index for accessing elements sequentially. Examples are integer array, character array etc.

Multidimensional arrays – An array with more than one dimensions. For example, a 3D array uses 3 indexes to access elements.

Jagged arrays – An array of arrays, where each inner array can be of different size. The lengths of each inner array vary.

Associative Array – An array where elements are accessed using strings or other objects instead of integers. Examples are maps or dictionaries.

Sparse arrays – An array where most elements are uninitialized or zero. It has few non-zero values interspersed.

Circular arrays – A linear array where end elements loop around to beginning seamlessly, creating a circular buffer.

Let’s look into each one of these in detail.

OneDimensional Array

A one dimensional array is the simplest type of array. It is a linear list of elements accessbile via indices.

Syntax for Declaration of One Dimensional Array

// Declaration of Single Dimensional Array

dataType[] arrayName;  

For example:

// Declaration of Single Dimensional Array Example

int[] marks; 

Initialization of Single Dimensional Array

One dimensional arrays can be initialized at the time of declaration like:

// Initialization of Single Dimensional Array Example

int[] numbers = {2, 4, 6, 8};

Or after declaration:

// Initialization of Single Dimensional Array Example

int[] nums;
nums = new int[]{1, 3, 5, 7, 9};

Accessing Elements of Single Dimensional Array

Access elements using index inside square brackets, specifying index of the cell to access

Note: Index of the cell starts from 0

int[] arr = {10, 20, 30, 40};
int first = arr[0]; // 10 
int second = arr[1]; // 20

Types of One-Dimensional Arrays

Different types of one-dimensional arrays are

  • Integer array – stores integers
  • Character array – stores characters
  • String array – stores string objects
  • Floating point array – stores floats and doubles

Multi-Dimensional Array

A multi-dimensional array has more than one dimension. It can be visualised as a table with rows and columns.

For example, a 2D array can be seen as a matrix or table. Similarly, a 3D array can be viewed as a cube of values.

Syntax for Declaration of Multi-Dimensional Array

Multi dimensional array can be declared using square brackets [], where number of square brackets depends on dimension of the array.

// Syntax for Declaration of Multi-Dimensional Array

dataType[][] arrayName; //2D array
dataType[][][] arrayName; //3D array 

TwoDimensional Array

A two-dimensional array is a type of multi dimensional array which stores elements in a tabular format with rows and columns.

Syntax and Declaration of Two Dimensional Array

Twodimensional array can be declared using square brackets [][] like:

// Syntax and Declaration of two dimensional Array

int[][] arr2D; 
// Declares a 2D integer array 

String[][] cities;
// Declares a 2D string array

Initialization of Two-Dimensional Array

There are multiple ways to intialize the two dimensional array For example:

// 1. At the time of declaration
int[][] matrix = { {1, 2, 3}, {4, 5, 6}};

// 2. After declaration

String[][] names = new String[3][2]; 
// 3 rows, 2 columns
names[0][0] = "John";
// Initialize element of 2D array

Accessing Individual Elements of Two-Dimensional Array

Each cell of Two dimensional array is located using row number and column number. To access an element specify row and column number of the cell.

Note: Row number and Column number starts with 0 indexing.

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

int firstElem = arr[0][0]; // 1
int thirdElem = arr[1][2]; // 6 

// Accessed using [row][col]

Three-Dimensional Array

A three-dimensional array is also a type of multi dimensional array A three-dimensional array can be visualized as a cube with three axes – height, width and depth.

Syntax for Declaration of Three Dimensional Array

Three dimensional array can be declared using square brackets [][][] l where each square bracket corresponds to one of the axes. For Example:

// Syntax for Declaration of Three Dimensional Array

int[][][] arr3D = new int[3][3][3];
// 3D array with 3 rows, 3 columns, 3 depths

The elements are accessed using [index1][index2][index3].

Implementation of Different Types of Arrays In Programming Languages

Here are some examples of declaring and initializing arrays in different languages:

Implementation of different types of arrays in Java

Some examples to implement different types of arrays in Java are:

//Implementation of different types of arrays in Java

int[] arr = {1, 2, 3}; // Single dimensional

int[][] matrix = {{1, 2, 3}, {4, 5, 6}}; // Multidimensional 

String[][] str2D = new String[2][3]; // Declare 2D array
str2D[0][1] = "Hello"; // Initialize 

Implementation of different types of arrays in C++

Some examples to implement different types of arrays in c++ are:

//Implementation of different types of arrays in C++

int arr[] = {1, 2, 3}; // Single dimensional

int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}}; // Multidimensional

string str2D[2][3]; // Declare 2D string array
str2D[0][1] = "Hello"; // Initialize

Implementation of different types of arrays in C sharp

Some examples to implement different types of arrays in c sharp are:

// Implementation of different types of arrays in c sharp

// One dimensional Array 
int[] arr = new int[5]; 
int[] numbers = {1, 2, 3, 4, 5};
int num = numbers[0];

// Two dimensional Array 
int[,] matrix = new int[2, 3];
int[,] points = { {1, 2, 3}, {4, 5, 6} }; 
int elem = points[0, 1];

Implementation of different types of arrays in C

Some examples to implement different types of arrays in c are:

//Implementation of different types of arrays in c

int arr[] = {1, 2, 3}; // Single dimensional

int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}}; // Multidimensional 

char str2D[2][3]; // Declare 2D array
str2D[0][1] = 'H'; // Initialize

Implementation of Array Of Arrays JavaScript

Here is an example of implementing a 2D array (array of arrays) in JavaScript:

// Declare a 2D array 
let matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];

// Access an element
console.log(matrix[1][0]); // Prints 4

// Modify an element
matrix[0][2] = 10; 

// Print the 2D array
for(let i=0; i<matrix.length; i++) {
  for(let j=0; j<matrix[i].length; j++) {
    console.log(matrix[i][j]); 
  }
}

// Output
// 1 2 10 
// 4 5 6
// 7 8 9

Int the above example:

  • Use nested arrays to create 2D arrays in JavaScript.
  • Access elements using matrix[i][j] notation.
  • Nested for loops can be used to iterate over the rows and columns.
  • Individual elements can be modified using the indexing.
  • Arrays don’t need size to be declared beforehand in JavaScript.

We can extend this to create multidimensional arrays with more nested levels like 3D arrays. The same principles will apply there as well.

Advanced Types Of Arrays

Let’s look into some other advance types of arrays used in various programming languages:

Jagged Arrays

A jagged array is an array of arrays, where each inner array can be of different length. For example:

// Jagged Arrays

int[][] jagged = new int[3][];

jagged[0] = new int[3]; 
jagged[1] = new int[2];
jagged[2] = new int[5];

Here the outer array has 3 elements, each being a separate inner array of different lengths. The sizes of inner arrays can vary, so it is “jagged”.

Associative Array

Associative arrays (hashes) are composed of key-value pairs. An associative array allows accessing elements using strings or other objects instead of numeric indices. For example:

// Associative Array

Map<String, Integer> ages = new HashMap();
ages.put("John", 25); 
ages.put("Sarah", 32);

int johnAge = ages.get("John");

In the above associative array name strings are used to index the ages map instead of numeric indices.

Sparse Arrays

A sparse array is an array where most elements are zero or uninitialized. For example:

// Sparse Array

int[] sparse = new int[10];
sparse[0] = 5; 
sparse[8] = 9;

Here only two elements are initialized while the rest remain zero.

Circular Array

A circular array is a linear array in which the end elements loop around to the start of the array. This creates a circular buffer where elements can be accessed in a cyclic fashion.  Circular arrays are used to implement queue. For example:

circular array
Circular Array
// Circular array of size 5
int[] circular = new int[5];  

int front = 0;
int rear = -1;
int count = 0; 

// Inserting elements 
// Modulo of 5 as array size is 5

rear = (rear + 1) % 5;  
circular[rear] = 10; 
count++;

rear = (rear + 1) % 5;
circular[rear] = 20;
count++;

// Accessing elements cyclically 
int element = circular[front]; 

front = (front + 1) % 5; 
element = circular[front];

Here the front and rear indices loop around 0-4 to provide circular access. By handling the fixed size array specially, we get a circular buffer abstraction on top.

To know about use cases of jagged arrays refer this

Summary and Notes on Types of Arrays And Their Representation

Array TypeDeclarationVisualizationAccessMemory Representation
One-dimensionaldataType[] arr;Lineararr[index]Contiguous
Two-dimensionaldataType[][] arr;Table/Matrixarr[i][j]Row/Column major
MultidimensionaldataType[][][] arr;Cube (3D)arr[i][j][k]Contiguous chunks
JaggeddataType[][] arr;Array of arraysarr[i].lengthNon-contiguous
AssociativeMap<K, V> map;Hash tablemap.get(key)Hash table buckets
Notes on Types of Arrays And Their Representation

To Summarize about types of array:

  • Arrays allow storing a collection of elements of the same type.
  • One dimensional arrays are the simplest with a single index.
  • 2D arrays are like matrices and can be visualized as tables.
  • 3D arrays add depth as the third dimension.
  • Arrays provide fast access to elements using indices.
  • Similar array declarations with slight syntax differences exist across languages like Java, C++, C#, C.
  • Multi-dimensional arrays are useful for structured data and matrix operations.
  • Jagged Arrays are Array of arrays, with inner arrays of differing sizes
  • A sparse array is an array where most elements are zero or uninitialized.

Try out our free resume checker service where our Industry Experts will help you by providing resume score based on the key criteria that recruiters and hiring managers are looking for.

Frequently Asked Questions On Types Of Arrays

What are the types of array in C?

In C, arrays can be:

  • One dimensional array (1D)
  • Two dimensional array (2D)
  • Multidimensional array (more than 2 dimensions)

What are two different types of arrays of arrays?

Two types of arrays of arrays are:

  • Jagged arrays – Arrays where each element is another array that can be of different sizes.
  • Multidimensional arrays – Arrays with fixed number of dimensions, like 2D or 3D arrays.

What types of arrays are supported by Javascript?

Javascript supports:

  • Indexed arrays – Simple arrays indexed with numbers
  • Associative arrays – Arrays indexed with strings or objects
  • Multidimensional arrays- Arrays with more than one dimension

Which types of arrays are always considered as linear arrays?

One dimensional arrays with a single index are always linear arrays.

How many types of arrays are there?

There are two main types of arrays based on dimensions – single dimensional arrays and multidimensional arrays. Multidimensional arrays can further have two dimensions (2D), three dimensions (3D) and so on. There some advanced types of arrays like jagged, sparse etc.

When does Incompatible types of assignment when assigning two arrays come?

This error occurs when the datatypes of the two arrays are different, like assigning an integer array to a character array. The array types should match.

90% of Tech Recruiters Judge This In Seconds! 👩‍💻🔍

Don’t let your resume be the weak link. Discover how to make a strong first impression with our free technical resume review!

Related Articles

Hello World! in C HackerRank Solution 

Problem Statement In this challenge, we will learn some basic concepts of C that will get you started with the language. You will need to use the same syntax to

Why Aren’t You Getting Interview Calls? 📞❌

It might just be your resume. Let us pinpoint the problem for free and supercharge your job search. 

Newsletter

Don’t miss out! Subscribe now

Log In