i need to create a multiplication table (array) of upto the number 20. anyone can help me? the user enters how many values they want. e.g. if its a 6 times table then the user wud enter 6.
youre confusing what the program output should look like, with what data it should contain
to make a 10 x 10 array:
int[][] my2D = new int[10][10];
-
to fill it with the times table, use 2 loops, one running from 0 to the length of the outer array
wrapped around, one running from 0 to the lenght of the inner array
Code:
for(i=0;i<outer.length;i++){
for(j=0;j<inner.length;j++){
//populate your array here
}
}
dont forget, that a 2 dimensional array, is an array, of arrays.. look at this image. each square is an array location. it will help you visualise how a 2D array works. it is an array of arrays.
array indexes become part of a variable name
String s0 = "string zero";
String s1 = "string one";
String s2 = "string two";
all those strings, are different just by the number. now suppose i did this:
String[] s = new String[3];
String s[0] = "string zero";
String s[1] = "string one";
String s[2] = "string two";
whats changed? well.. nothing really, except a few extra []. its still a name, yes?
when a proper number is combined with the name of the array, it becomes part of the name. the only difference is, we can insert the number during the operation of the program, instead of before compilation, yes?
so you accept that s[0] is just the name of a string, just like s0 is.
the loops will count like this:
i=0
i=0,j=0
i=0,j=1
i=0,j=2
i=1,
i=1,j=0
i=1,j=1
.. and so on...
the important thing here is that the first loop, with I in it, deals with the first array.. the array that holds arrays
so each name in that ss[0], ss[1] and ss[2] .. is the name of another array. and it is that another array that holds the strings.. so to get to the string.. we put an index on the name.
remember that the name is ss[0] .. put an index on it:
ss[0][0]
the bold bit is the index that gets us access to the string, the other bit is the name of the current array.. see how this works, with arrays of arrays?