Skip to main content

Single and Multidimensional arrays in Java

In this blog we are going to talk about arrays in java and how to use them. In the first part of this following blog, we will explain arrays and give a brief introduction of syntax and uses in java and further move forward to see multi-dimensional array concept and how to use it in java.

The java is a very robust programming language and provides use with many better options like array-list and vectors. These are dynamic implementations of the well-known array data structure. But knowing the basics of array and its related concept is very important. So knowing this lets move forward and take a look at the importance of array.

________________________________________________________

Before we go any further, let's look at why we need Java Array in the first place: 

  • Arrays are a common data storage structure. 
  • Arrays in Java allow us to store multiple objects of the same type. 
  • It may be used in conjunction with a loop to access items by index.

 

Arrays

Introduction:

Arrays in Java are objects that represent homogeneous data structures. Arrays are data structures that contain one or more values of a given data type and allow for indexed access to those values. An array element's index is used to access it. Arrays are a useful way to bring together relevant data.

Types of Array in java

One-Dimensional Arrays :

The syntax for the 1D Array is as follows:

    Syntax :type variable-name[];

    OR

    type[] variable-name;

Examples of 1D arrays:

public class Array1{
    public static void main(String[] args){
        int[] array1 = new int[5];
        array1[1] = 5;
        System.out.println(array1[1]);
    }} 

Multidimensional Arrays:

Multidimensional arrays are collections of arrays, with each array member referring a different collection of arrays. This form of array is also known as jagged arrays. A multidimensional array is created by appending one set of square brackets ([]) to each dimension.

This visual might help in understanding the multi-dimensional

array and its indexing.


The Syntax for 2D Array is as follows:
    Syntax :type variable-name[][];

    OR

    type[][] variable-name;

For more detailed explanation regarding the use of syntax check out the following example:

Example no1:

public class Multi_Array {
    public static void main(String[] args) {
        int[][] array = {{1,2},{3,4}};
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length; j++) {
                System.err.println(array[i][j]);
            }
        }
    }
}

Example no2:

public class Array_2D {
    public static void main(String[] args) {
        int[][] arr = new int[3][2];
        arr[1][0] = 5;
        System.out.println(arr[1][0]);
   }
}

How Indexing works in Multi-Dimensional Array?

Let's see how indexing for the 2D array works. It might seem scary at first but once we get a hold of it, it will seem very intuitive and easy.


So to get a good idea let's check out this example:

In the given example we will see how 2D Array treats each of the array that is contained inside of it. We can even use methods like we use on 1D Array and calculate the length.

public class Array_2d1 {
    public static void main(String[] args) {

        int[][] a = {
            {1, 2, 3},
            {4, 5, 6, 9},
            {7, 8, 9, 10},
        };
     
        System.out.println(a[0].length);
        System.out.println(a[1].length);
        System.out.println(a[2].length);
    }
}

Tabular representation of a two-dimensional array: 

A two-dimensional array is a table with 'x' rows and 'y' columns, with row numbers ranging from 0 to (x-1) and column numbers ranging from 0 to (y-1). 

 Accessing and printing of 2D array:

You already know that you may assign a value to each of the 2d array's individual elements when you initialize it. This is accomplished by accessing a specific element using the array's row and column indexes. You may retrieve the value of an individual element and print it to the user in the same way that you can initialize it.

Example for printing of 2D Array:

public class Array_2d1 {
    public static void main(String[] args) {

        int[][] arr1 = {
            {1, 2, 3},
            {4, 5, 6, 9},
            {7,8,9,10},
        };
        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr1[i].length; j++) {
               System.err.println(arr1[i][j]);
            }
        }
    }
}


3D Array and its implementation

Now we have already understood what a 2D array actually is, so using the same concept we can create a 3D array. By intuition the 3D array would a array that stores a array of array. Now it may first sound a bit weird but actually its just as simple as a 2D array. Now, lets take a visual look at what we discussed earlier regarding the 3D array.


Now that let's take a look at the coding example on how to declare a 3D array:

public class Array_3D {
    public static void main(String[] args) {
        //Declaring a 3D array in java
        int[][][] arr3 = new int[2][2][2];
    }
}

Now we will look at an example on how to print and access the the elements in the 3D array:

public class Array_3D {
        public static void main(String[] args) {
            int[][][] arr3 = {{{0,1},{3,2},{8,6}},{{1,10},{9,5},{4,7}}};    
            for(int i=0; i < arr3.length; i++){
              for(int j=0; j < arr3[i].length; j++){
                 for(int k=0; k < arr3[i][j].length; k++){
                    System.out.print( arr3[i][j][k] + " ");
                 }
                 System.out.println();
              }
              System.out.println("#######################################");
        }
    }
}


Conclusion:

In conclusion we would to say that array is a very important data structure and is a very important part of java programming language. Java has many advanced implementations of the static arrays we learned now namely like array-list and vectors which have similar concepts as that of static single and multidimensional array. That tells us that array even though is a simple structure but is very effective and useful. The concept of multidimensional array is very simple and intuitive if we could visualize it.






















Comments

  1. Replies
    1. Muddo bhai...kab aara phir aapka blog ? 😘

      Delete
  2. Found helpfull.....
    Got many things to learn from this blog.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. https://grabify.link/SZ1KW6
      Plz suppot me 🀧

      Delete
  4. Great work mate
    Very informative

    ReplyDelete
  5. Very informative....Keep it upπŸ‘

    ReplyDelete
  6. Really helpful ,was very informative.

    ReplyDelete
  7. Cleared mine whole Concept... Awesome...

    ReplyDelete
  8. Replies
    1. https://grabify.link/SZ1KW6

      Plz suppot me 🀧 paste this link in your browser to show your suppot πŸ™

      Delete
  9. Palan hardy...themks for your appreciation πŸ™ pranam

    ReplyDelete
  10. Keep up this Good Work, really very informative blog. Looking forward for More such information blog

    ReplyDelete
  11. Very helpful and informative .

    ReplyDelete

Post a Comment

Popular posts from this blog

OPERATIONAL AMPLIFIER APPLICATIONS

OPERATIONAL AMPLIFIER APPLICATIONS     A high-gain, the direct-coupled amplifier is what an operational amplifier is. It has two inputs and an output, but generally, it is operated in single-ended input - single-ended output mode. Op-amp can perform several arithmetic operations like addition, subtraction, differentiation, integration, comparison, analog to digital conversion, etc. Op-amps are used in analog computers.    IC 741 is a widely used OP-Amp. In this IC when the input is zero, the output can be adjusted to zero by varying the 10kΩ potentiometer between ‘offset null’ terminals.    Applications of OP-Amp : There are many applications of Op-amp. It has a high open-loop gain, high input impedance, and low output impedance. It has a common-mode rejection ratio. Due to these favorable characteristics, it is used for different applications. Important Linear applications are- buffer, inverter, adder, and subtractor. Important non-linea...

Protection and Security in RTOS

Differences between OS and RTOS:  Flowchart Explaining Structure of a typical Realtime Operating System                                                                                         Operating systems are the interface between the hardware of the system and the applications running on the system.  A computing environment with the capacity to respond to events within a strictly defined time frame is presented by real-time operating systems (RTOS). Modern embedded systems for particular domains (like aerospace, industrial control, defense, and medicine) contain safety-critical programmes whose failure would have disastrous consequences. T...