Wednesday, May 25, 2022

Single Array VS Multidimensional Array


A collection of related data pieces stored in contiguous memory locations is referred to as an array. It is the most basic data structure in which each data element may be retrieved simply by its index number alone. For example, if we want to keep a student's grades in five subjects, we don't need to establish distinct variables for each topic. We can instead create an array that stores the data items in contiguous memory locations.

This blog will serve as a resource for practically everything you need to know about arrays, including the differences between single and multidimensional arrays.

What is a Single and Multidimensional Array?

➢Single Array: 

It is nothing but an object that holds the elements having same data type.

➢Multidimensional Array:

In simple terms, we can describe multidimensional arrays as an array of arrays.

Single Array:

      When it comes to Java arrays, an array is a collection of like-typed variables i.e elements having same data type. Java arrays are different than arrays in C/C++. Some important points regarding java arrays are listed below.

    In Java, all arrays are allocated dynamically.
o Because arrays are objects in Java, we may use the object attribute length to determine their length. This differs from C/C++, where we use sizeof to find length.
o   A Java array variable can also be declared like other variables with [] after the data     type.
o   The array's variables are sorted, and each has an index starting at 0.
o   A static field, a local variable, or a method parameter can all be utilised with a Java array.
o   An array's size must be given using an int or short integer rather than a long number.
o   The direct superclass of an array type is Object.
o   Every array type implements the Cloneable and java.io interfaces. Serializable.

     Depending on the array's definition, it can include both primitive (int, char, etc.) and object (or non-primitive) pointers to a class. The actual values for primitive data types are stored in contiguous memory regions. The real objects are kept in a heap segment for class objects.

➢The general form of a one-dimensional array declaration is :

type var-name[];

OR

type[] var-name;

➢Initializing an Array in Java

        Only a reference to an array is produced when an array is declared. You make an array like this to create or provide memory to the array: When it comes to one-dimensional arrays, the general form of new is as follows:

var-name = new type [size];

int[] intArray = new int[20];

        When an array is declared, all that is produced is a reference to it. To generate or contribute memory to the array, you create an array like this: When it comes to one-dimensional arrays, new takes the following form:

➢Accessing Java Array Elements using for Loop

        The index of each element in the array is used to access it. The index starts at 0 and goes all the way to (total array size)-1. Java for Loop allows you to access all of the elements in an array.

for (int i = 0; i < arr.length; i++)

  System.out.println("Element at index " + i + " : "+ arr[i]);

Multidimensional Array:

        The simplest version of a multidimensional array is a two-dimensional array. For ease of understanding, we can think of a two-dimensional array as an array of one-dimensional arrays.

➢ Data in multidimensional arrays are stored in tabular form.




                                                Fig. Tabular Presentation Of 2-D Array

➢Declaration

● General form of declaring N-dimensional arrays:

data_type array_name [size1][size2]....[sizeN];

○ data_type: The data type that will be placed in the array.

○ array_name: Name of the array.

○ size1, size2,... ,sizeN: Dimensions and their sizes.

➢Examples:

○ Two dimensional array:

int two_d[10][20];

○ Three dimensional array:

int three_d[10][20][30];

    We can declare a two dimensional integer array say ‘x’ of size 10,20 as: int x[10][20];

    Elements in two-dimensional arrays are commonly referred by x[i][j] where i is the row number and ‘j’ is the column number.

A two – dimensional array can be seen as a table with ‘x’ rows and ‘y’ column

○where the row number is in the range of 0 to (x-1)

○ and column number ranges from 0 to (y-1).

Initializing Two – Dimensional Arrays:

A Two-Dimensional array can be initialised in one of two ways.

First Method:

● int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}.

There are three rows and four columns in the aforementioned array.

○ In the table, the components in the braces are retained in the same order as they occur in the braces.

 ○ The array will be filled in order, starting with the first four elements from the left in the first row, followed by the next four elements in the second row, and so on.

Better Method:

 int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};

○ Nested brackets are used in this type of initialization.

 ○One row is represented by each set of inner braces.

○Because there are three rows in the previous example, there are three sets of inner braces.

➢Accessing Elements of Two-Dimensional Arrays:

In two-dimensional arrays, row and column indexes are utilised to access elements.
● Example: int x[2][1];
● In the example above, the element in the third row and second column is represented.
● Note: In arrays, if the array size is N,. Its index will be from 0 to N-1. Therefore, for row index 2 row number is 2+1 = 3.
● To print all the entries in a Two-Dimensional array, we can use nested for loops. We'll need two loops for the loops. One moves through the rows, while the other moves through the columns.

Three – dimensional Array (3D-Array)

        A three-dimensional array is a more sophisticated version of a multidimensional array. For ease of understanding, a three-dimensional array can be thought of as an array of two-dimensional arrays.

Indirect Method of Declaration:

Declaration – Syntax:

data_type[][][] array_name = new data_type[x][y][z];

        For example: int[][][] arr = new int[10][20][30];

Initialization – Syntax:

array_name[array_index][row_index][column_index] = value;

        For example: arr[0][0][0] = 1;

 Accessing Elements of Three-Dimensional Arrays:

        Elements in three-dimensional arrays are commonly referred by x[i][j][k] where ‘i’ is the array number, ‘j’ is the row number and ‘k’ is the column number.

        3D array representation in tabular format: A three – dimensional array can be seen as a tables of arrays with ‘x’ rows and ‘y’ columns where the row number ranges from 0 to (x-1) and column number ranges from 0 to (y-1). A three – dimensional array with 3 array containing 3 rows and 3 columns is shown below:

                                        Fig. Representation of 3-D array.

Comparison Between Types Of Arrays in Java

       An array is a data structure that allows you to store variables of similar data kinds in close proximity. The array's key benefit is random access and cache friendliness. The array can be divided into three types:

·       One Dimensional (1D) Array

·       Two Dimension (2D) Array

·       Multidimensional Array

·      ➢ One Dimensional Array:

o   It's a collection of variables with comparable data kinds.

o   It allows for random access, and all of the elements can be found by their index.

o   The array's size is predetermined.

·    ➢Two Dimensional Array:

o   It's a collection of lists of the same data type variable.

o   It also allows for random access, and all of the elements can be found by their index.

o   It can alternatively be thought of as a set of 1D arrays. The Matrix is another name for it.

o   Its size can be expanded from 2 to 3, then 4, and so on.

o   They're all known as multi-dimensional arrays.

A 2D array is the most common multidimensional array.


Differences On The Basis Of Different Criteria:

o   Criteria

One Dimensional Array

Multidimensional Array

Definition

Store a single list of the element of a similar data type as a single dimensional array.

Store a single list of the element of a similar data type as a two or multidimensional array.

Representation

Represent multiple data items as a list.

Represent multiple data items as a table consisting of rows and columns.

Dimension

One

Atleast two or more than two

Size(bytes)

size of(datatype of the variable of the array) * size of the array

size of(datatype of the variable of the array)* the number of rows* the number of columns.

Address calculation.

Address of a[index] is equal to (base Address+ Size of each element of array * index).

Address of a[i[[j] can be calculated in two ways row-major and column-major

Column Major: Base Address + Size of each element (number of rows(j-lower bound of the column)+(i-lower bound of the rows))

Row Major: Base Address + Size of each element (number of columns(i-lower bound of the row)+(j-lower bound of the column))

Example

int arr[5];  //an array with one row and five columns will be created.

 

{a , b , c , d , e}

int arr[2][5];  //an array with two rows and five columns will be created.

 

               a  b  c  d  e

 

               f  g   h  i   j


Real Time Application Of Arrays and Multidimensional Arrays:

o     Arrangement of the leader-board of a game can be done simply through arrays to store the score and arrange them in descending order to clearly make out the rank of each player in the game.
o   A straightforward inquiry, The paper consists of a series of numbered questions, each of which is assigned a score.
o   In image processing, 2D arrays, sometimes known as matrices, are employed.
o   It's also utilised in speech processing, where each speech signal is represented by an array.
o   A multidimensional collection of pixels also makes up your viewing screen.
o   In a Library Management System, book titles are stored.
o   Purchasing tickets via the internet.
o   On a cell phone, there are contacts.

THANKYOU!
REFERENCE:
1]https://ieeexplore.ieee.org/document/7237077?arnumber=7237077
2] https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
3]https://practice.geeksforgeeks.org/
4]https://www.javatpoint.com/

22 comments:

  1. Amazing write-up Team! The way you cover this topic in a single blog with excellent content is really appreciated. Keep it up.

    ReplyDelete
  2. Well written and informative blog indeed!

    ReplyDelete
  3. GREAT SHARE !!!
    Superb Very creative Superior work .

    ReplyDelete
  4. Just make it simple and anybody can easy understand about array. And in the presentation of the information is well and quite good...I really inspiring.. great work...🔆

    ReplyDelete
  5. Clear my point related to arrays

    ReplyDelete
  6. Very useful information well written in simple form got my all points cleared

    ReplyDelete
  7. very helpfull to clear the concepts

    ReplyDelete
  8. Very useful content good work

    ReplyDelete
  9. Hard work...! Helpful Content

    ReplyDelete
  10. Helpful stuff... Gr8 work!!

    ReplyDelete
  11. I was Suicidal but now after reading this I have this hope that I can too do something and rise up against this shallow capitalistic society. Thanks to whoever wrote this I owe him or her my life. Please post another article on "pseudocode"

    ReplyDelete
  12. This article is helpful for me.

    ReplyDelete
  13. This article is very useful for Exam purpose ..keep posting ..

    ReplyDelete
  14. Amazing and helpful content, it's very informative 👍

    ReplyDelete
  15. thanks for uploading.....
    I was searching for this

    ReplyDelete

Single Array VS Multidimensional Array

A collection of related data pieces stored in contiguous memory locations is referred to as an array. It is the most basic data structure in...