TechArena Community

TechArena Community (http://forums.techarena.in/)
-   Software Development (http://forums.techarena.in/software-development/)
-   -   How to implement MultiDimensional Array in JAVA (http://forums.techarena.in/software-development/1131767.htm)

Nihar Khan 28-02-2009 01:11 PM

How to implement MultiDimensional Array in JAVA
 
Hello Programming Gurus,

I am newbie to the Multidimensional Array topic in JAVA it has alway been difficult for me to understand so think here i will get some help.
So can anybody suggest how should i implement Multidimensional Array in JAVA, explain me with some example.

Regards,

Bantu 28-02-2009 01:17 PM

Re: How to implement MultiDimensional Array in JAVA
 
Here i will explain you with the small example on MultiDimensional Array in JAVA
please have a look at it.

Code:

int[][] a2 = new int[10][5];
// print array in rectangular form
for (int r=0; r<a2.length; r++) {
    for (int c=0; c<a2[r].length; c++) {
        System.out.print(" " + a2[r][c]);
    }
    System.out.println("");
}


Astro121 28-02-2009 01:22 PM

Re: How to implement MultiDimensional Array in JAVA
 
A multi-dimensional array of dimension n (i.e., an n-dimensional array or simply n-D array) is a collection of items which is accessed via n subscript expressions. For example, in a language that supports it, the tex2html_wrap_inline60030 element of the two-dimensional array x is accessed by writing x[i,j].

The Java programming language does not really support multi-dimensional arrays. It does, however, support arrays of arrays. In Java, a two-dimensional array x is really an array of one-dimensional arrays:

int[][] x = new int[3][5];

The expression x[i] selects the tex2html_wrap_inline57420 one-dimensional array; the expression x[i][j] selects the tex2html_wrap_inline59650 element from that array.

The built-in multi-dimensional arrays suffer the same indignities that simple one-dimensional arrays do: Array indices in each dimension range from zero to tex2html_wrap_inline59952, where length is the array length in the given dimension. There is no array assignment operator. The number of dimensions and the size of each dimension is fixed once the array has been allocated.

Jannat 28-02-2009 01:25 PM

Re: How to implement MultiDimensional Array in JAVA
 
The type-signatures and constructors of the multidimensional array use double brackets to distinguish them from ordinary arrays:

int [[,]] a = new int [[5, 5]] ;

float [[,,]] b = new float [[10, n, 20]] ;

int [[]] c = new int [[100]] ;

a, b and c are respectively 2-, 3- and one- dimensional arrays. Of course c is very similar in structure to the standard array d, created by

int [] d = new int [100] ;

c and d are not identical, though3.

Accessing individual elements of a multidimensional array goes through a subscripting operation involving single brackets

for(int i = 0 ; i < 4 ; i++)
a [i, i + 1] = i + c [i] ;

For reasons that will become clearer in later sections, this style of subscripting is called local subscripting. In the current sequential context, apart from the fact that a single pair of brackest may include several comma-separated subscripts, this kind of subscripting behaves just like ordinary Java array subscripting. Subscripts always start at zero, in the ordinary Java or C style (there is no Fortran-like lower bound).


All times are GMT +5.5. The time now is 04:25 PM.