Types of Arrays
Arrays can be classified in two ways:
- On the basis of Memory Allocation
- On the basis of Dimensions
Types of Arrays on the basis of Memory Allocation:
1. Static Arrays:
In this type of array, memory is allocated at compile time having a fixed size of it. We cannot alter or update the size of this array. This type of memory allocation is also known as static or compile-time memory allocation. Here only a fixed size (i,e. the size that is mentioned in square brackets []) of memory will be allocated for storage. In case, we don’t know the size of the array then if we declare a larger size and store a lesser number of elements will result in a wastage of memory or or we declare a lesser size than the number of elements then we won’t get enough memory to store all the elements. In such cases, static memory allocation is not preferred.
//Static Integer Array
int arr[5] = {1, 2, 3, 4, 5};
//Static Integer Array
int arr[5] = {1, 2, 3, 4, 5};
// Static Array
int[] arr = { 1, 2, 3, 4, 5 };
import array
# Static Array
arr = array.array('i', [1, 2, 3, 4, 5])
// Static array
int[] arr = { 1, 2, 3, 4, 5 };
let arr[] = {1, 2, 3, 4, 5}
2. Dynamic Arrays:
In this type of array, memory is allocated at run time but not having a fixed size. Suppose, a user wants to declare any random size of an array, then we will not use a static array, instead of that a dynamic array is used. This type of memory allocation is also known as dynamic or run-time memory allocation. It is used to specify the size of it during the run time of any program.
// Dynamic Integer Array
int* arr = new int[5];
// Dynamic Integer Array
int* arr = malloc(sizeof(int) * 5);
// Dynamic Integer Array
ArrayList<Integer> arr = new ArrayList<>();
# Dynamic Array
arr = []
// Dynamic Integer Array
int[] arr = new int[5];
// Dynamic Array
let arr = new Array(5);
Types of Arrays on the basis of Dimensions:
1. One-dimensional Array(1-D Array): You can imagine a 1d array as a row, where elements are stored one after another.
2. Multi-dimensional Array: A multi-dimensional array is an array with more than one dimension. We can use multidimensional array to store complex data in the form of tables, etc. We can have 2-D arrays, 3-D arrays, 4-D arrays and so on.
- Two-Dimensional Array(2-D Array or Matrix): 2-D Multidimensional arrays can be considered as an array of arrays or as a matrix consisting of rows and columns.
- Three-Dimensional Array(3-D Array): A 3-D Multidimensional array contains three dimensions, so it can be considered an array of two-dimensional arrays.
Getting Started with Array Data Structure
Array is a collection of items of the same variable type that are stored at contiguous memory locations. It is one of the most popular and simple data structures used in programming. In this article, we have decided to provide a complete guide for Arrays, which will help you to tackle any problem based on Arrays.
Table of Content
- What is an Array?
- Basic terminologies of array
- Memory representation of Array
- Declaration of Array
- Initialization of Array
- Importance of Array
- Types of arrays
- Operations on Array
- Complexity Analysis of Operations on Array
- Advantages of Array
- Disadvantages of Array
- Applications of Array
- Top theoretical interview questions
- Frequently Asked Questions (FAQs) on Arrays:
Contact Us