array, linked list, stack, queue Flashcards
What is an array?
An array is a collection of items stored at contiguous memory locations. The idea is to store multiple items of the same type together. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array).
Array Index
In an array, elements are identified by their indexes. Array index starts from 0.
Array Element
Elements are items stored in an array and can be accessed by their index.
Array Length
The length of an array is determined by the number of elements it can contain.
The representation of an array can be defined by its declaration. A declaration means
allocating memory for an array of a given size.
Array (Array Elements) ->
2 8 14 3 6 9
0 1 2 3 4 5 (Array Index)
int arr[5];
// This array will store integer type element
char arr[10];
// This array will store char type element
float arr[20];
// This array will store float type element
C++ and C arrays
/* Static Arrays are arrays that are declared before runtime
and are assigned values while writing the code.
*/
// The syntax of declaring a static array is:
<data><variable>[]
= {<data1>, <data2>,…..<dataN> };
// Example:
int arr[] = { 2, 5, 6, 9, 7, 4, 3 };
</dataN></data2></data1></variable></data>
Java array
int[] arr = new int[5]; // This array will store integer type element
C# array
let arr=[10,20,30];
// This array will store integer
let arr2 = [‘c’, ‘d’, ‘e’]
// This array will store characters
let arr3 = [28.5, 36.5, 40.2]
// This array will store floating elements
JavaScript array
arr = [10, 20, 30]
# This array will store integer
arr2 = [‘c’, ‘d’, ‘e’]
# This array will store characters
arr3 = [28.5, 36.5, 40.2]
# This array will store floating elements
Python array
static or compile-time memory allocation
the array element’s memory is allocated when a program is compiled. Here only a fixed size (i,e. the size that is mentioned in square brackets []) of memory will be allocated for storage
It is possible to allocate memory dynamically. So, dynamic memory allocation is the process of
assigning the memory space during the execution time or the run time.
int *array = new int[5];
Dynamic array C++
// <data> <variable> [ <Total>];
// Example:
int arr[10];
// Store integer elements
String arr[5];
// Store String type of elements</Total></variable></data>
Dynamic array c++
Empty list
list of integers
my_list = [1, 2, 3, 4]
my_list = []
my_list = [“Hello”, 1, 5.5]
Dynamic array Python3
int[] numArray = new int[] {};
Dynamic array C#
// Create array using literal
var x = [“a”, “b”, “c”];
// Create array using constructor
var x = new Array();
// Create array using constructor with items
var y = new Array(“a”, “b”, “c”);
JavaScript has dynamic arrays: their size is not predetermined, nor the type of data.
$arr = array(“a”,”b”,”c”);
PHP dynamic array
One-dimensional array (1-D arrays)
You can imagine a 1d array as a row, where elements are stored one after another.
Two-dimensional array
2-D Multidimensional arrays can be considered as an array of arrays or as a matrix consisting of rows and columns.
Col 0 1 2
0 5 4 10
1 2 3 44
2 11 1 2
Three-dimensional array
A 3-D Multidimensional array contains three dimensions, so it can be considered an array of two-dimensional arrays.
arr[2][row][col] contains two 2-d arrays (arr[0][row][col] and arr[1][row][col])
Array operation Traversal
Traverse through the elements of an array.
Array operation Insertion
Inserting a new element in an array.