Skip to main content

Multidimensional Array in Java

Welcome back to my blog! In my previous post you learned about single dimensional array in Java.
A multidimensional array is a fixed sized sequenced collection of data of a same data type.
In multidimensional array, data is stored in rows and columns. A multidimensional array is actually an array of arrays.


Syntax to declare a multidimensional array:

datatype array_name[][];


Syntax to instantiate a multidimensional array:

int array[][] = new int[4][4];

This will allocate 4 rows and 4 columns.


Syntax to initialize a multidimensional array:

array[0][0] = 1;
array[0][1] = 2;
array[0][2] = 3;
array[0][3] = 4;
array[1][0] = 5;
array[1][1] = 6;
array[1][2] = 7;
array[1][3] = 8;
array[2][0] = 9;
array[2][1] = 10;
array[2][2] = 11;
array[2][3] = 12;
array[3][0] = 13;
array[3][1] = 14;
array[3][2] = 15;
array[3][3] = 16;


For printing the multidimensional array:

for(int i=0;i<4;i++;) {
for(int j=0;j<4;j++) {
System.out.println(array[i][j]+"");
         }
System.out.println();
      }





Now, let's see an example of a multidimensional array:

class JavaArray {
public static void main(String[] args) {
          int m_array[][] = new int[4][4]; //declaring and instantiating
          m_array[0][0] = 1; //initializing the array
          m_array[0][1] = 2;
          m_array[0][2] = 3;
          m_array[0][3] = 4;
          m_array[1][0] = 5;
          m_array[1][1] = 6;
          m_array[1][2] = 7;
          m_array[1][3] = 8;
          m_array[2][0] = 9;
          m_array[2][1] = 10;
          m_array[2][2] = 11;
          m_array[2][3] = 12;
          m_array[3][0] = 13;
          m_array[3][1] = 14;
          m_array[3][2] = 15;
          m_array[3][3] = 16;

for(int i=0; i<4; i++) {
for(int j=0; j<4; j++) {
System.out.println(array[i][j]+"");
}
System.out.println();
                           }
                     }
               }


Thank you for coming to my blog. Now you have learned how to declare, instantiate, initialize and print a multidimensional array.

If you find this post helpful, please share it with your friends and colleagues.

FEEL LIKE A PROGRAMMER!!!

Comments

Popular posts from this blog

VLOOKUP in MS Excel

VLOOKUP in MS Excel H ello and welcome back to Learn Java. It's been very long since I wrote my last post. This post will give you a short and simple tutorial on using VLOOKUP in MS Excel. Verticle Lookup or VLOOKUP is a function in MS Excel that helps a user look for a specific value in a column   based on another particular value in the first column. A simple example: Your boss asks you to find the price of a food item based on its name. Syntax: VLOOKUP(the value you want to look up, where you want to look for it(array/column/table), the column number the value is in, return an approximate or exact match) Let's try to understand it more clearly: Here's a little data set containing a table of my favourite food items.  Step 1: Select a specific cell/text value you would like to search for in the given table/data set. Here, we are looking for the price of Pizza, so we use cell value "Pizza" which is also cell reference A3. We are making sure to use a cell value/re

Simple Java program

J ava is a high level programming language which is used almost everywhere- in Android mobile phones, set top boxes, in Google Maps, video games, etc. Today let's see how to make the simplest Java program- "Hello, world" in Eclipse. Step #1. Select your file location where you want to save your work. Step #2. Select File>New>Project as shown in the picture below: Step #3. Select Java>Java Project>Next as shown below: Step #4. Enter the project name in Project name as shown below: Step #5. Select Finish. Step #6. Select Yes. Step #7. Double click one the project name. You will see a folder named as "src" under the the project HelloWorld. Step #8. Right click on the folder "src">New>Class. Step #9. Enter the name of the class. Then click on Finish. Step #10. You will see a screen like this: Step #11. Write the following code: public class Hello { public static void main(String[] args) { System.out.println("Hello, world!!"); }