Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Multidimensional arrays in Java

Fri Oct 10, 2008 12:07 am

Multidimensional arrays
If you are a java developer so it is for sure you are dealing with arrays, in java you can create 1, 2 or 3 dimensional arrays, in this article we go throw how to use multidimensional arrays in java. For memory allocations, java uses an approach named “Arrays of Arrays” where java establishes a multidimensional array from a group of one dimensional arrays and not like other approaches that reserve a blocks in memory.



1-dimensional arrays:
For one 1dimensional array you will have only one row and you will have to specify its length in allocation step. You first will declare a 1D array as follows:

Code:
int[] array1D; // Declares  1D array.       


And you do memory allocation as follows:
Code:
array1D = new int[5]; // memory allocation       


You do these two steps in only one:

Code:
int array1D = new int[5]; 



This creates a one array of 5 elements. Note that the index start from zero, following snippet fills a 1D array and prints its content:
Code:
  array1D[0]=54;
            array1D[1]=11;
            array1D[2]=43;
            array1D[3]=74;
            array1D[4]=84;


            for(int i=0;i<array1D.length;i++)
            {
                System.out.println("Element#"+(i+1)+" - "+array1D[i]);
            } 


The output is as follows:
Code:
Element#1 - 54
Element#2 - 11
Element#3 - 43
Element#4 - 74
Element#5 - 84




2-dimensional arrays
For creating 2D dimensional arrays you will need to define the number of rows and columns in your array, the declaration and allocation is as follows:

Code:
int[][] array2D = new int[3][6]; 


The previous line creates a 2d dimensional array; this actually creates 7 objects, one dimensional array of 6 elements for each for the rows, and a one dimensional array of 3 elements. Following snippet adds data and prints it to a 2D array:
Code:

    int
[][] array2D=  new int[3][6];
            array2D[0][4]=54;
            array2D[1][5]=11;
            array2D[2][2]=43;
           

            System
.out.println("array2D.length: "+array2D.length);
            System.out.println("array2D[0].length: "+array2D[0].length);
            for(int i=0;i<array2D.length;i++)
                for(int j=0;j<array2D[0].length;j++)
            {
                System.out.println("Element#"+(i+1)+","+(j+1)+" - "+array2D[i][j]);
            }
 

The output of this snippet is as follows:

Code:
array2D.length: 3
array2D[0].length: 6
Element#1,1 - 0
Element#1,2 - 0
Element#1,3 - 0
Element#1,4 - 0
Element#1,5 - 54
Element#1,6 - 0
Element#2,1 - 0
Element#2,2 - 0
Element#2,3 - 0
Element#2,4 - 0
Element#2,5 - 0
Element#2,6 - 11
Element#3,1 - 0
Element#3,2 - 0
Element#3,3 - 43
Element#3,4 - 0
Element#3,5 - 0
Element#3,6 - 0


3-dimensional arrays
It works same as 2d dimensional array as you can do allocation as follows:

Code:
int[][][] array3D=  new int[3][6][7]; 




Re: Multidimensional arrays in Java

Tue Jul 24, 2012 2:50 pm

updated...

Post a reply
  Related Posts  to : Multidimensional arrays in Java
 Multidimensional Arrays in JSP     -  
 Arrays in java     -  
 creating arrays in java     -  
 Comparing Arrays in java     -  
 How to compare two arrays in java     -  
 Passing arrays as function parameter in java     -  
 multidimensional array merge using PHP     -  
 Concept of arrays     -  
 Arrays in photoshop     -  
 Defining arrays in ASP     -  

Topic Tags

Java Arrays