Array vs List

By | February 6, 2019

Array and List are two data structure used to store multiple values. The main difference between both is that the Array is a collection of Homogeneous data elements whereas the List is a Heterogeneous collection of data elements. The list can be homogeneous or non-homogeneous, it can contain different data type elements at once. The homogeneous and heterogeneous data elements are nothing just as per their name specifies that homogeneous data elements refer to the same data types that means all the elements are in the array will be of same data type and heterogeneous is its vice versa.

Array

An array is a data structure which contains homogenous elements or data type. Elements in an array stored in consecutive order. Almost, all the High-level Programming languages have the concept of Build-in array.

Syntax in different languages to define an Array

Syntax to define an Array in C++

#include <conio.h>
#include <iostream.h>
void main()
{
int arr[20];
for(i=0;i<20;i++)
cin>>arr[i];
for(i=0;i<20;i++)
cout<<arr[i];
}

In the above code we have declared an array arr of size 20, so the indices value of this array arr would be from 0 to 19.

The syntax of Array using NumPy in Python

Python does not have the concept of the array but you can use the array in Python by importing it from its library i.e. known as NumPy.

Syntax:
from numpy import*
arr = array([1,2,3,4,5,6,7,8])

Array declaration in Java

class ARRAY
{
public static void main (String[] args)
{
int[] arr;
arr = new int[5];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
for (int i = 0; i < arr.length; i++)
System.out.println("Element at index " + i +" : "+ arr[i]);
}
}

Features of Array

  • Homogeneous collection of elements
  • Total bytes = sizeof(Datatype) * size of array
  • The Array stored the element in an ordered sequence, that’s why an array is called linear data Structure.
  • The array has the property of finiteness this refer that every array contains a finite number of elements.
  • The elements of the array can be accessed by its relative index number
  • Array always stored in a contiguous memory location.

Operations to Perform on Arrays:

  • Accessing
  • Inserting
  • Deleting
  • Traversing

Advantages and Disadvantages of Array

Advantages

  • You can store multiple data elements by the same name using an array
  • An array is very fast to use.
  • With the help of the index, you can access any element with the time complexity of O(1).
  • Other data structure can also implement by using array such as list, stacks, queue, etc.
  • The mathematics calculation like the matrix is easy on arrays.
  • You can find the address of any element of an array for that you just need to know the address of the first element.
  • 2-D arrays used to represent matrices.

Disadvantages

  • Array is finite.
  • The finiteness of array can lead the problem of memory wastage if you declare the size of array more than the elements to store.
  • As the Array used to store in the contiguous memory location, if you have a memory space that is enough for your array but not in a contagious manner then you won’t be able to initialize your array.
  • The array size cannot increase and decrease during runtime.
  • The time complexity of insertion and deletion in the array is O(n).

[su_button url=”http://www.techgeekbuzz.com/python-list-array/” background=”#1263c6″ size=”8″ center=”yes” radius=”round”]Difference between Python Array and List[/su_button]

List

Lists are similar to the array which is also used to store elements. The only difference is that list can store heterogeneous data elements too. You can perform many tasks on the List, however, it has some shortcoming too. But the operations performed on the list are more efficient than the arrays.

Key Features of the list:

  • List elements do not store in the contagious memory location.
  • A list is a collection of data and elements.
  • A collection of elements is simply termed as a Node.
  • Each node contains a memory address of the Successor Node.
  • The first node of list termed as the Head Node.
  • If a list does not contain any value is termed as an Empty List.
  • The last node of the list known as Tail Node
  • The tail node pointer point to NULL, that signifies the end of the list.

The operation to be Perform on the List

  • Inserting new element
  • Deletion
  • Traversing
  • Sorting
  • Searching for a given element
  • Checking the size of the list.
  • Checking whether the list is empty or not.
  • Copying a list to another list.

Advantages and Disadvantages of List

Advantages

  • List are dynamic Data Structure that is capable of adding and deleting new element during runtime.
  • List is immune to memory wastage; in the list, you can define the size of the list according to the elements required.
  • It’s easier to perform the Insertion and Deletion operations on the list.
  • Other data structures such as Stacks and Queues can be implemented through List.

Disadvantages:

  • As compared to array, list require more memory space because of additional pointer element.
  • Traversal in list is difficult.
  • You cannot access any element directly.

Leave a Reply

Your email address will not be published. Required fields are marked *