What is an array?
- An array is a collection of data items
- These items are similar types
- An array can also be called a list
Properties an array
- An array can contain a group of elements of a similar type
- Array elements are stored in contiguous memory blocks
- Arrays use numeric indexing
- Arrays index starts with0 and ends with size-1
- Size of the array can be dynamic and static
- Element of an array can be accessed using its index
Syntax of an array
- The array can be defined with help of square brackets
- It varies from language to language but square brackets are used to define an array
// Define array in c int prices[4] = {10, 20, 30, 40}; // Define array in Javascript let prices = [10, 20, 30, 40]; // Define array in python prices = [10, 20, 30, 40]
Accessing an element
- An element can be accessed using an index
- Let’s access an element
// Define array in Javascript let prices = [10, 20, 30, 40]; console.log(prices[2]); // 30
Updating an index value
- An array index value can be updated by overriding a value
- Let’s update some values
// Define array in Javascript let prices = [10, 20, 30, 40]; // Update index o value to 5 prices[0] = 5; console.log(prices); // [5, 20, 30, 40]