Results 1 to 20 of 29
- 01-24-2011, 09:07 AM #1
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
hi guys please help me in "for loop "
OUTPUT :Java Code:class TwoDArray { public static void main(String args[]) { int twoD[][]= new int[4][5]; int i, j, k = 0; for(i=0; i<4; i++) /* i could not understand how this "for loop" works .please make me clear guys */ for(j=0; j<5; j++) { twoD[i][j] = k; k++; } for(i=0; i<4; i++) { for(j=0; j<5; j++) System.out.print(twoD[i][j] + " "); System.out.println(); } } }
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19Last edited by funkygarzon; 01-24-2011 at 10:10 AM.
- 01-24-2011, 09:31 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
use code tags when posting code it makes it easier to read
However, you are populating and printing a 2d array, so one loop is for the array of array position, and the other is populating each array in the array.
When thinking of a 2d array try thinking of a chess board, the first loop points to the current row and the second loop puts a value in each square.
Since its a 4 by 5 array, there is an array with 4 items, and each item in the array contains another array with 5 items.
So the first loop will be array[0][another array]
the second loop will fill another array and it continues until each item in the array is filled.
Another way to clarify is this, imagine only having one loopJava Code:array[4][5]: array[0][0] = 0 array[0][1] = 1 array[0][2] = 2 ... array[0][5] = 5 ... array[4][5] = 19
What do you think would happen here?Java Code:for(int i = 0; i < arr.length; i++){ arr[i][i] = k++; }
- 01-24-2011, 10:09 AM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
thanks for your reply sunde887 ,
my doubt is
how this "for loop "is working ? i mean their is no curly braces "{" after "for(i=0;i<4;i++)" and the curly braces starts only from "for(j=0;j<5;j++)" . my doubt is how the i value is getting incremented without being inside curly braces ? . i hope you understand me :(Java Code:for(i=0; i<4; i++) /* i could not understand how this "for loop" works .please make me clear guys */ for(j=0; j<5; j++) { twoD[i][j] = k; k++;
- 01-24-2011, 10:16 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
ah, I see, Id imagine it's because you don't have to use a statement block
works, so I guess since the only expression after the first for loop is another for loop it works correctly, I could be wrong, and if I am I'm sure someone will correct me, but I'm fairly confident this is why.Java Code:for(int i = 0; i < 5; i++) System.out.println(i);
- 01-24-2011, 10:28 AM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
If a for loop is not followed by braces, then its body is simply the next statement -- in this case, that statement is the next for loop. Note that this is legal syntax, but bad and confusing style.
This:
...is the same as this:Java Code:for(i=0; i<4; i++) for(j=0; j<5; j++) { twoD[i][j] = k; k++; } // more code here
Style and indenting are important, especially when you are starting out. Note the space after the for keyword -- it's not a method. Note the spaces around the assignment and comparison operators -- they make the code easier to read.Java Code:for (i = 0; i < 4; i++) { for (j = 0; j < 5; j++) { twoD[i][j] = k++; // no need for the extra increment statement } } // more code here
-Gary-
- 01-24-2011, 10:31 AM #6
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
- 01-24-2011, 12:25 PM #7
sunde887 was right, even if he clarified himself a bit loopy :p
If you dont know what I mean by "all this is that next line":Java Code:for(i=0; i<4; i++) <-- This takes ONLY the next line as a body of the loop for(j=0; j<5; j++) { \ twoD[i][j] = k; \ all this k++; / is that next line } /
Java Code:for(j=0; j<5; j++) {twoD[i][j] = k; k++;} <-- It could also be written this way
- 01-24-2011, 12:50 PM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Sometimes I tend to get like 50 thoughts at a time and express them poorly, I apologize.
- 01-24-2011, 12:56 PM #9
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
wooooooooooooooooooooooooooooooooooooooooooooow amazing this is what i want buddy , i clearly understood demoniac . now i wonder how this "for(i=0;i<4;i++) " loop is getting incremented without any curly brace when j becomes j=4 ?
wooooooow thanks buddy this was another wonderful explanation ..you just put the curly brace and made me understand ,... thanks again
- 01-24-2011, 01:01 PM #10
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
i got another doubt in "for loop " , so please check below code and explain me buddy ,
Java Code:When you allocate memory for a multidimensional array, you need only specify the memory for the first (leftmost) dimension. You can allocate the remaining dimensions separately. For example, this following code allocates memory for the first dimension of twoD when it is declared. It allocates the second dimension manually. int twoD[][] = new int[4][]; twoD[0] = new int[5]; twoD[1] = new int[5]; twoD[2] = new int[5]; twoD[3] = new int[5];
what is the meaning for this and how we have visualize this in a "chess board " like sunde887 said ?Java Code:int twoD[][] = new int[4][];
and how thesecodes really works ?Java Code:twoD[0] = new int[5]; twoD[1] = new int[5]; twoD[2] = new int[5]; twoD[3] = new int[5];
Last edited by funkygarzon; 01-24-2011 at 01:04 PM.
- 01-24-2011, 01:13 PM #11
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
My chessboard example was just something I used when I first encountered 2d arrays, for me it clarifies them a bit. It's more of how I visualize a 2d array in my head. an array like
That would be like a chessboardJava Code:int[][] chess = new int[8][8];
if you did
output would be:Java Code:for(int i = 0; i < arr.length; i++){ for(int j = 0; j < arr[i].length; j++){ arr[i][j] = 1; } } for(int i = 0; i < arr.length; i++){ for(int j = 0; j < arr[i].length; j++){ System.out.print(arr[i][j] + " "); } System.out.println(); }
64 items, just like an 8x8 chessboard would haveJava Code:1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
The loop increments i because it's how a for loop works, it continues until the condition is no longer met. When j reaches 4, the second loop is finished, so the statement for the first loop is finished and i can increment.
Try thinking of a multi-dimensional array as a single array, normally an array would contain numbers, or some other data. In a multi-dimensional array, each element conatains an int array.
So you are allowed to declare the size of each item after the initial array declaration.
I hope I am being clear.Last edited by sunde887; 01-24-2011 at 01:20 PM.
- 01-24-2011, 01:14 PM #12
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
This is subtle and confusing, but in a sense, there is really no such thing as a "multi-dimensional" array. What you really have are arrays of arrays. So this:
...is the same as this:Java Code:int[][] twoD = new int[3][5];
...which is the same as this:Java Code:int[][] twoD = new int[3][]; for (int i = 0; i < 3; i++) { twoD[i] = new int[5]; }
You could alternatively do something like this:Java Code:int[][] twoD = new int[3][]; twoD[0] = new int[5]; twoD[1] = new int[5]; twoD[2] = new int[5];
All these things are legal, but they're very unusual, and not worth thinking about all that much. Stay away from weird, confusing code, and if you must do something weird, comment it profusely.Java Code:int[][] twoD = new int[3][]; twoD[0] = new int[2]; twoD[1] = new int[10]; twoD[2] = new int[6];
-Gary-
- 01-24-2011, 01:41 PM #13
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
oh sunde887 , that was great buddy ...thanks for making me clear ..thank you :)
nice explanation buddy , but i am like understood but not so clearly understood .
please tell me according to this below code
int[][] twoD = new int[3][];
twoD[0] = new int[2];
twoD[1] = new int[10];
twoD[2] = new int[6];
where " twoD[1] = new int[10]; " this "10" allocation happens . i mean in which row and in which column ? sorry for my noob question and for my bad english
- 01-24-2011, 01:51 PM #14
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
with this code, each element in the array twoD contains an array, so the element at 0 has an array containing 2 items, the element at 1 contains an array with 10 items, and element 3 contains an array with 6 itemsJava Code:int[][] twoD = new int[3][]; twoD[0] = new int[2]; twoD[1] = new int[10]; twoD[2] = new int[6];
The array would print out as:
and you can print it with this codeJava Code:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Java Code:int[][] arr = new int[3][]; arr[0] = new int[2]; arr[1] = new int[10]; arr[2] = new int[6]; int k = 1; //instantiate items in array for(int i = 0; i < arr.length; i++){ for(int j = 0; j < arr[i].length; j++){ arr[i][j] = k++; } } //print items for(int i = 0; i < arr.length; i++){ for(int j = 0; j < arr[i].length; j++){ System.out.print(arr[i][j] + " "); } System.out.println(); }
- 01-24-2011, 03:53 PM #15
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
wooooooooooooooooooooooooooooooooooooooooooooooooo w buddy thank you very much , you made me understand this clearly :) .
so for the above code ,Java Code:int[][] twoD = new int[3][]; twoD[0] = new int[1]; twoD[1] = new int[5]; twoD[2] = new int[2];
OUTPUT IS THIS RIGHT :
now new doubt a raised for me buddy , sorry ,Java Code:1 2 3 4 5 6 7 8
how does the compiler is taking the length correctly if we give "arr.length" ? and what is the difference between "arr.length " and " arr[i].length" ? ...sorry for too much request sunde887 ...thanks in advanceJava Code:arr.length
Last edited by funkygarzon; 01-24-2011 at 04:02 PM.
- 01-24-2011, 05:29 PM #16
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Haha, I don't mind, ask as much as you want, I refresh these forums way more than I should
ok, arr.length in a multidimensional array gets the length, put better, it counts how many arrays there are
in this code, arr.length counts how many i's there are, in this case there is 0, 1, and 2, so arr.length will produce 3.Java Code:int[][] arr = new int[3][]; int[0] = new int[2]; int[1] = new int[5]; int[2] = new int[3];
Now, how would you determine the length of the second piece? you can't use the same number all the time, you need a .length type item. Since each item in the array contains an array arr[i].length will find the length of the array stored in element i.
I may be unclear, let me know how you understand this, and if it's confusing I will do my best to clarify it.Java Code:int[][] = new int[3][]; //arr.length = 3, it is declared here int[0] = new int[7];//arr[i].length here means arr[0].length, and in this one its length is 7 int[1] = new int[3];//arr[i].length is 3 int[2] = new int[100];//arr[i](arr[2]).length = 100
I believe you are overcomplicating multidimensional arrays, someone else above said "there really is no such thing as a multi dimensional array, it's actual an array where the elements contain arrays." Thinking like that might help you in understanding. Use the chessboard example for picturing what they look like, and use this description to think how they are defined.
Think about an array of strings, if you wanted to loop through each string in the array with str.substring, what would you do?
its basically the same as a 2d array
If this extra part is confusing just ignore it and focus on the stuff above it.Java Code:for(int i = 0; i < arr.length; i++){ for(int j = 0; j < arr[i].length() - 1; j++){ arr[i].substring(i, i + 1); } }Last edited by sunde887; 01-24-2011 at 05:36 PM.
- 01-25-2011, 06:29 AM #17
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
wooooooooooooooooooooooooooooooooooooooooooooooooo ooooooooooooow wonderful great explanation - i clearly i understood the difference buddy . :) . thank a lot for spending time for making me understand clearly :)
now i could not figure out the result for the below code which you gave as string example :(
particularly this lineJava Code:for(int i = 0; i < arr.length; i++){ for(int j = 0; j < arr[i].length() - 1; j++){ arr[i].substring(i, i + 1); } }
i haven 't seen like this before ? what is the meaning for this "arr[i].length() - 1 " and why you are using method " () " here ? :(Java Code:arr[i].length() - 1
Last edited by funkygarzon; 01-25-2011 at 06:31 AM.
- 01-25-2011, 06:45 AM #18
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 01-25-2011, 06:49 AM #19
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
- 01-25-2011, 08:55 AM #20
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
ooohh my dear friends , Another big doubt
1 . what is 3 by 4 by 5 matrix ? i could not visualize the matrix of this below program
This program generates the following output:Java Code:// Demonstrate a three-dimensional array. class threeDMatrix { public static void main(String args[]) { int threeD[][][] = new int[3][4][5]; int i, j, k; for(i=0; i<3; i++) for(j=0; j<4; j++) for(k=0; k<5; k++) threeD[i][j][k] = i * j * k; for(i=0; i<3; i++) { for(j=0; j<4; j++) { for(k=0; k<5; k++) System.out.print(threeD[i][j][k] + " "); System.out.println(); } System.out.println(); } } }
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12
0 0 0 0 0
0 2 4 6 8
0 4 8 12 16
0 6 12 18 24
Alternative Array Declaration
Similar Threads
-
Mutliple JPanel's with an enhanced "for" loop
By javaman1 in forum New To JavaReplies: 4Last Post: 10-23-2010, 12:18 AM -
An "if" statement inside a "for" loop?
By soccermiles in forum New To JavaReplies: 18Last Post: 04-20-2010, 03:44 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
How do we "Loop" through subfolders to delete files?
By Zrob in forum New To JavaReplies: 3Last Post: 09-26-2008, 06:05 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks