Not sure exactly what your question is about. Maybe you should clarify? Especially since i + 1 doesn't appear. From what your code shows, I suspect the file should be written as follows (fewer magic numbers, reasonable formatting and not missing brackets/content):
class Test{ public static void main( String [] args ) { int [][] twoD = new int[5][]; twoD[0] = new int[1]; twoD[1] = new int[2]; twoD[2] = new int[3]; twoD[3] = new int[4]; twoD[4] = new int[5]; int i, j, k = 0; for( i = 0; i < twoD.length; i++ ) { for( j = 0; j < twoD[i].length; j++ ) { twoD[i][j] = k; k++; } } for( i = 0; i < twoD.length; i++ ) { for( j = 0; j < twoD[i].length; j++ ) { System.out.print( twoD[i][j] + " " ); System.out.println(); } } } }
Ahh, you've updated your code. The i + 1 you are talking about only works here because each second depth array has a length of 1 greater than the index it is coming from.
twoD[0] contains an int array of length 1. twoD[1] contains an int array of length 2. twoD[2] contains an int array of length 3.
The i + 1 ONLY works for this specific example. The code I posted above, which uses array[i].length will work even if you change the sizes of the arrays. I would strongly recommend following my example.
@BenWalker, U r ryt, i have copied this code from java refernce book by herbert schildt, Yeah the code seems a bit random, Coz for one statement he doesn't use curly braces in the for loop... Thx Both Of U guys for your sweet replies... PEACE
@Benwalker U r ryt, I willl use that technique now, he has also used the same technique that u showed me in your example, I dnt know why he confused me with i+1...
@BenWalker I have compiled your code, but it has a problem, It comes in a staight line, Like this, 0 1 2 3 4 5 BUt code that i showed u, with i+1 comes out to be this, 0 1 2 3 4 5 6 7 8 9
class Test{ public static void main( String [] args ) { int [][] twoD = new int[5][]; twoD[0] = new int[1]; twoD[1] = new int[2]; twoD[2] = new int[3]; twoD[3] = new int[4]; twoD[4] = new int[5]; int i, j, k = 0; for( i = 0; i < twoD.length; i++ ) { for( j = 0; j < twoD[i].length; j++ ) { twoD[i][j] = k++; System.out.print( twoD[i][j] + " " ); } System.out.println(); } } }
As has been pointed out to you in another thread, this isn't a java forum. I am primarily a java programmer, so am happy to help occasionally, but you are posting off-topic, so to speak.
I would advise a) getting a better book (Schildt's work is reasonable, but error prone) and b) thinking more about what you are doing. Books, especially texts that provide examples such as these, are trying to teach you how a language works, not provide you with building blocks for software. Try to learn exactly what an array is and how it works, how loops work (especially the conditional logic that underpins them) and how the primitive data types (including Strings) work.
A useful exercise in arrays (please do use them!) and conditional logic, as well as input/output methods, is to write your own "Mastermind" game. Game rules can be found here:
twoD[0] contains an int array of length 1.
twoD[1] contains an int array of length 2.
twoD[2] contains an int array of length 3.
The i + 1 ONLY works for this specific example. The code I posted above, which uses array[i].length will work even if you change the sizes of the arrays. I would strongly recommend following my example.
0
1
2
3
4
5
BUt code that i showed u, with i+1 comes out to be this,
0
1 2
3 4 5
6 7 8 9
As has been pointed out to you in another thread, this isn't a java forum. I am primarily a java programmer, so am happy to help occasionally, but you are posting off-topic, so to speak.
I would advise a) getting a better book (Schildt's work is reasonable, but error prone) and b) thinking more about what you are doing. Books, especially texts that provide examples such as these, are trying to teach you how a language works, not provide you with building blocks for software. Try to learn exactly what an array is and how it works, how loops work (especially the conditional logic that underpins them) and how the primitive data types (including Strings) work.
A useful exercise in arrays (please do use them!) and conditional logic, as well as input/output methods, is to write your own "Mastermind" game. Game rules can be found here:
http://en.wikipedia.org/wiki/Mastermind_(board_game)
I won't be offended if you don't give it a go, but do consider it :-)