Hi Shawn, Thank you for the explanation. Would it be possible to extend the exaple to 3 dimensional arrays ? I am still quite confuse because I am not sure how the 3rd index might be represented. Another question is : how to adress the legth of a 3 dimensional array? Andreea
*-------------------------------------------------------------------------------------------- Faith can move mountains! Anything's Possible—Keep Thinking --- On Mon, 2/23/09, Shawn Ayromloo <[email protected]> wrote: From: Shawn Ayromloo <[email protected]> Subject: [java programming] Re: array 2 dimension size To: [email protected] Cc: [email protected], [email protected], [email protected] Date: Monday, February 23, 2009, 3:41 AM The previous reply might be misleading, so I bother everyone with my reply to all. Since Java is similar to C/C++ in many ways, unlike Fortan, two--dimensional arrays are arranged in row-major not column-major in Java. This means that the 1st index refers to a row, not a column and the 2nd index refers to a column, not row. This means that the following declaration: int[][] a = new int[3][4]; and the following initialization: a[0][0] = 1; a[0][1] = 2; a[0][2] = 3; a[0][3] = 4; a[1][0] = 5; a[1][1] = 6; a[1][2] = 7; a[1][3] = 8; a[2][0] = 9; a[2][1] = 10; a[2][2] = 11; a[2][3] = 12; produces: [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] Also, row-major arrangement dictates that traversing the array in the following manner achieves maximum cash efficiency: public class MultiDimensionalArrayExample { public static void main(String[] args) { int a[][] = new int[][]{{1,2,3,4}, {5,6,7,8}, {9,10,11,12}}; for(int i = 0; i < 4; i++) { for(int j = 0; j < 3; j++) { System.out.println(a[i][j]); } } } } Shawn Ayromloo Néstor wrote: The 1st part in a multidimensional array indicates the column (vertical) position The 2nd part in a multidimensional array indicates the row (horizontal) position The 3rd part in a multidimensional array is the depth position Array[4]x[3]: [][][][] [][][][] [][][][5] The value '5' is in position [2][3] Array[4]x[3]x[3]: [] [] []/ [][][][] [5][][]/ [][][][] The slash is used to indicate the depth or 3rd dimension of the array The value '5' is in position [1][0][2] I hope this helps :-) On Sun, Feb 22, 2009 at 9:16 AM, <[email protected]> wrote: Franz: I will answer as many questions as I can: 1. The memory required for a 4x3 array of int would be 4 bytes per integer x 4 x 3 = 48 bytes total. 2. The easiest way to fill a 3D array like that would probably be with nested-for loops. i.e.: for (int i = 0; i < 50; i++) { for (int j = 0; j < 128; j++) { for (int k = 0; k < 256; k++) { num[i][j][k] = ... } } } 3. I can't think of an easy way to explain this one. A two-dimensional array is an "array of arrays" (for example, from question #1, an array of 4 arrays of 3 int's). A 3D array is an expansion from that (an array of arrays of arrays). Sorry, I can't think of another way to say that. 4. I prefer to think of that situation as 50 rows, 128 columns and 256 "layers". If you think of a box sitting on the floor, the length of the box would be, for example, the rows (say 50), the width of the box would be the columns (128), and the third number would be how far off of the floor (256). Don't know if that helps or not, but that's how I picture it. 5. How much memory is consumed would depend on the values that you are storing in the array. It would be (50x128x256) x the size of the type of data you're storing (4 for int, 1 for boolean, etc.). 6. I can't help you on this one. I haven't gotten up to GUI or AWT yet. Cheers! Eric On Feb 22, 2009 7:34am, franzgabriel <[email protected]> wrote: > > > Somebody help me please, I still struggling to understand array multi > > Dimension, > > My question is : > > > > 1. if I create 2 dimensional array 4 x 3 and the data type integer in > > 32 bit computer how much memory will consume? Is it still only 4 byte > > or become doubler ?? > > > > 2. how to fill values in row & column after initialized fixed > > dimension size in 3 D array: > > > > int [ ][ ][ ] num; > > num = new int [50][128][256]; > > > > 3. What does it means 50 x 128 x 256 in array 3 D? > > > > 4. in array 3D 50 is become Row, 128 is column, and the 256 is column, > > Is it right? > > > > 5. from above no.3 question the total is 1.651.200 field to store data > > value, How much a memory will consume? > > > > 6. What does array use for in our java programming actually in GUI > > (AWT)?? only store a value or any different purpose? > > > > > > > > > --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en -~----------~----~----~----~------~----~------~--~---
