Quick Code
Published in

Quick Code

Lets Answer #2

What are Arrays?

Let's demystify Arrays.

Photo by Luca Bravo on Unsplash

An array is a data structure that stores a bunch of similar typed data in a contiguous memory location. This means that the array elements can be traversed with the help of whole number indexing.

A visualization for Arrays. Picture source: Oracle.

Each data type takes a particular amount of memory. Some of the values are:

+----------------+----------------------------+
| Data types | Memory Occupied(in Bit) |
+----------------+----------------------------+
| Boolean | 1 |
| Byte | 8 |
| Char | 16 |
| Short | 16 |
| Int | 32 |
| Long | 64 |
| Float | 32 |
| Double | 64 |
+----------------+----------------------------+

In C and C++, the size of Char is 8 Bit while in Java it is 16 bit as C and C++ use ASCII format and Java uses Unicode Format.

Calculation of address of each element

The formula for calculation of address of each element is

Address of nth element = Base Address +(n-1)*(size of data type) 
where
n is the element you want the address for.
Base address is the address of element at first element
and size of data type refers to memory occupied by the data type.

When to use arrays:

  • When the order of data matters.
  • When you know the number of elements and the number of elements is fixed.
  • And when the elements are similarly typed. An array can only hold a single data type.

Note: In most of the languages, the index starts from 0 but in some languages, the index starts with one. So all the limits for the loops will change accordingly.

Arrays are one of the most basic data structures and can be used to implement other data structures like Queue, stack, heap, etc.

Basic operations on Arrays

Here are some of the basic operations which can be performed on arrays:

In Java, the arrays are abstracted by the class called ArrayList. Using this class is beneficial as it provides various functions and helps you to write better code.

Traversing

Traversing means printing all the elements of a data structure.

Sorting

There are multiple ways to sort an array. Some of the most popular ways of sorting arrays are Bubble Sort, Selection Sort, Merge Sort, Quick Sort, Radix Sort, Heap Sort, Bucket Sort, and many more. Sorting will be covered in a separate article as it is a pretty huge topic.

Searching

Searching if the element is present in the array. If it is present, it will return the index, or else it will return -1

Reversing

Reversing the order of the arrays. This means that the last element will be swapped by the first element.

There are more operations like Insertion, Deletion, check if it is a palindrome, etc. They will be covered in subsequent articles.

Stay tuned for more articles and if in case you have missed the previous article on “Introduction to Data Structures”. Do check it out. The link to the article is here.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store