create array upto 20 integers


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: create array upto 20 integers

  1. #1
    Join Date
    Mar 2004
    Posts
    7

    create array upto 20 integers

    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.

  2. #2
    Join Date
    Dec 2002
    Posts
    83
    Please either post the code you have so far, or ask a specific question as to what part you need help on.
    -- Steven

  3. #3
    Join Date
    Mar 2004
    Posts
    7
    just on how to write the code which asks the user how many times table it is and then initialises that array.


    1 2 3 4 5

    1 1 2 3 4 5

    2 2 4 6 8 10

    3 3 6 9 12 15

    4 4 8 12 16 20

    5 5 10 15 20 25


    thats how it shud look like

    but in straight rows and columns

  4. #4
    Join Date
    Feb 2004
    Posts
    808
    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.

    heres how you use them in a loop:
    Code:
    for(int i=0; i<s.length; i++){
      s[i] = "string "+i;
    }
    java will replace the i with 0, then 1, then 2.. all the time, amking names

    -

    now, its no different for 2D arrays.. except that this time, our first array isnt going to hold Strings.. its going to hold other arrays (see the pic)

    String[][] ss = new String[3][3];

    now instead of just one loop, we use 2 loops:
    Code:
    for(int i=0;i<ss.length; i++){
      for(int j=0; j<ss[i].length; j++){
        ss[i][j] ....whatever..
      }
    }
    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?


    hheres the pic:
    Attached Images Attached Images
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  5. #5
    Join Date
    Feb 2004
    Posts
    808
    heres another different pic that will help you understand how 2d arrays work. it emphasises the importance of what is the name, and what is the index:
    Attached Images Attached Images
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center