inserting values into a multi-dim array


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: inserting values into a multi-dim array

  1. #1
    Join Date
    Oct 2004
    Posts
    34

    inserting values into a multi-dim array

    This method is instantiated by a multi-dim array, and creates an array of objects:
    Triangle[] triangleArray = new Triangle[4];

    int[][] a1 = {
    {3, 4, 5,},
    {1, 2, 3,},
    {3, 3, 3,},
    {2, 2, 3,},
    };

    for(int i=0; i <= triangleArray.length-1; i++){
    triangleArray[i] = new Triangle(a1[i][0],a1[i][1],a1[i][2]);
    }

    How can I enter the values into the array using the JOptionPane.showInputDialog box? I see how I can do it with one array[] but, not sure on how to insert into a multi-dim array object.

  2. #2
    Join Date
    Sep 2004
    Posts
    150
    I believe what you need are called nested for loops.


    Let's say you had a two dimensional array of strings you wanted to print:

    String[][] books = new String[10][10];

    PHP Code:
    // Printing each entry would look like this:

    for (int i 0<10i++)
    {

      for (
    int j 0<10i++)
      {

    System.out.println(books[i][j]);

      }

    }


    // for three dimensions it would look like this:

    for (int i 0<10i++)
    {

      for (
    int j 0<10j++) 
      {

        for (
    int k 0<10k++)
        {

    System.out.println(books[i][j][k]);
        }

      }


    Note that all the work is done in the inner most loop, this won't always be the case but it should for what you're doing.


  3. #3
    Join Date
    Oct 2004
    Posts
    34

    inserting values into a multi-dim array

    I understand how to process the array...but I want to enter the values into the array from a dialog box...as opposed to hard code the array....as follows. So the user will enter: 3, dialog box pops up, then 4 and so on...

    int[][] a1 = {
    {3, 4, 5,},
    {1, 2, 3,},
    {3, 3, 3,},
    {2, 2, 3,},
    };

    for(int i=0; i <= triangleArray.length-1; i++){
    triangleArray[i] = new Triangle(a1[i][0],a1[i][1],a1[i][2]);
    }
    hope thats clear
    Thanks...

  4. #4
    Join Date
    Sep 2004
    Posts
    150
    Then do one loop and for each iteration of that loop enter the three values for the triangle, one after the other, in the same loop body.

    Observing that you don't seem to need that multidimensional array of values (since you're constructing the triangles on the fly); It would look like this:

    PHP Code:

    for(int i=0triangleArray.lengthi++){


    int base Integer.parseInt(JOptionPane.showInputDialog("Please input a base value"));

    int width Integer.parseInt(JOptionPane.showInputDialog("Please input a width value"));

    int height Integer.parseInt(JOptionPane.showInputDialog("Please input a height value"));

    triangleArray[i] = new Triangle(base,width,height);


  5. #5
    Join Date
    Oct 2004
    Posts
    34

    Thumbs up inserting values into a multi-dim array

    Awesome! I was getting hang up on using the two dimensional array when it was not necessary when constructing triangles on the fly.
    Thanks!

  6. #6
    Join Date
    Mar 2004
    Posts
    635
    To make it more user-friendly, I'd suggest having the user enter the values like so: "3,78,32,9,34" And then you would parse the string. That way the user doesn't have to click OK on 50 million dialog boxes.

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