Array for Data structure.
In programming, an array is a data structure that stores a collection of elements of the same type stored at a contiguous memory location. The elements in an array are typically accessed by their index, which represents their position and the total number of elements in an array is said to be length.
Why do we need an Array?
Grouping Related Data: Arrays allow you to group related data together in a single structure.
Sequential Access: Arrays provide a way to access elements in a sequence using their index.
Efficient Storage and Retrieval: Elements in an array are stored in contiguous memory, allowing for quick access based on index.
Performing Operations on Collections: Arrays enable performing operations on multiple elements simultaneously, like iteration, transformation, and filtering.
Sorting and Searching: Arrays offer methods and algorithms for sorting and searching elements.
Implementation of Data Structures: Arrays serve as the foundation for implementing various data structures like stacks, queues, heaps, and hash tables.
Declare Array.
Array declaration is different in different languages.
In cpp.
int myArray[5] = {1, 2, 3, 4, 5};
In Java.
int[] myArray = {1, 2, 3, 4, 5};
// both are valid declarations
int myArray[];
or int[] myArray;
In Python.
myArray = [1, 2, 3, 4, 5]
In Javascript
// Using the [] brackets
let myArray = [1, 2, 3, 4, 5];
// Using the Array() constructor
let myArray = new Array(1, 2, 3, 4, 5);
int number [4] = {1,5,7,11}
// here 4 state the length.
// Example
int array [10000] ={}
// It will initialize array with garbage value.
// Case 2
int array [100000] ={0}
//It will initialize the array with 0 to its all indexes.
// Task 1
//How to initialize entire array with any values.
Note: In general we declare elements of same datatype/data structure in an array but in the case of Python, Javascript, Php, Ruby, C# elements can be of a different datatype/ data structure.
Indexes and Accessing element in Array
In Array index starts with 0.
let myArray= [1,2,3,4,5,6]
console.log(myArray[0]) // accessing via index
// output 1
console.log(myArray[1]) // accessing via index
// output 2
console.log(myArray[2]) // accessing via index
// output 3
console.log(myArray[3]) // accessing via index
// output 4
console.log(myArray[4]) // accessing via index
// output 5
console.log(myArray[5]) // accessing via index
// output 6
Initialization in Arrays
Languages like Python, JavaScript, and Ruby allow for easier array initialization compared to C++ and Java. In Python, JavaScript, and Ruby, we can simply declare an array without specifying its length beforehand. However, in C++ and Java, it is necessary to provide the length of the array during declaration.
int number [4] = {1,5,7,11}
// here 4 state the length.
// Example
int array [10000] ={}
// It will initialize array with garbage value.
// Case 2
int array [100000] ={0}
//It will initialize the array with 0 to its all indexes.
// Task 1
//How to initialize entire array with any values.
Thank you for exploring, make sure to do task 1.
Subscribe to my newsletter
Read articles from Roshan Guragain directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Roshan Guragain
Roshan Guragain
I am a Software developer who is passionate about making web app using the technologies like Html, Css, Javascript, React, Mongodb, Mongoose, Express, Node.