This is a discussion on Arrays in Java within the Java Programming forums, part of the Software Development category; Hi all, let us start explonation and samples of 2 dimensional array and multidimensional array in java here. Anybody can ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Multi-dimensional arrays Java, as with most languages, supports multi-dimensional arrays - 1-dimensional, 2-dimensional, 3-dimensional, ... This discusses 2-dimensional arrays, but the same principles apply to higher dimensions.
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| 2-dimensional arrays 2-dimensional arrays are usually represented in a row-column approach on paper, and the terms "rows" and "columns" are used in computing. Arrays of arrays There are two ways to implement 2-dimensional arrays. Many languages reserve a block of memory large enough to hold all elements of the full, rectangular, array (number of rows times number of columns times the element size). Java doesn't do this. Instead Java builds multi-dimensional arrays from many one-dimensional arrays, the so-called "arrays of arrays" approach. [C++ supports both styles.] There are a couple of interesting consequences of this: Rows may be different sizes. Also, each row is an object (an array) that can be used independently.
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Declaration Declare a 2-dimensional array as follows: Code: int[][] a2;
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Allocation As with all arrays, the new keyword must be used to allocate memory for an array. For example, Code: int[][] a2 = new int[10][5]; This actually allocates 6 objects: a one-dimensional array of 5 elements for each of the rows, and a one-dimensional array of ten elements, with each element pointing to the appropriate row array.
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Sure… Often 2-dimensional arrays are processed with nested for loops. Notice in the following example how the rows are handled as separate objects. For example 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("");
}
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Using Arrays in PHP | Sabari | PHP Programming | 127 | 10-20-2008 08:44 PM |
| Jagged Arrays in C# | vigneshgets | C# Programming | 4 | 09-30-2008 04:48 AM |
| Classic asp arrays and recordset | ramesh123 | ASP and ASP.NET Programming | 1 | 12-02-2007 08:44 PM |
| Split Arrays in ORACLE | Murali | Database Support | 2 | 08-22-2007 11:34 PM |
| Java:Tutorial - Arrays | pranky | Java Programming | 0 | 02-24-2007 12:54 AM |