Results 1 to 3 of 3

Thread: 2D array

  1. #1
    bluekswing is offline Member
    Join Date
    Jun 2007
    Posts
    14
    Rep Power
    0

    Default 2D array

    I have a two dimensional array where each row of the array holds string values that represent attributes of an object. I'm trying to figure out how to iterate over each row setting each element in a row equal to a corresponding object's instance variables.


    So I want to Instaniate an object, set that objects instance variables equal to the corresponding values in a row of my 2d array - (then either do something with the object or store it in a data structure for later use) and repeat. Either way, I"m getting stumped at how to iterate appropriately over each row and set an object's instance variables accordingly.


    Thanks for the help as always!

  2. #2
    ShoeNinja's Avatar
    ShoeNinja is offline Senior Member
    Join Date
    Oct 2007
    Posts
    124
    Rep Power
    0

    Default

    It seems like you just need a for loop.
    Java Code:
    for(int i = 0; i < array[0].length; i++){
      //use array[0][i]
      //use array[1][i]
    }
    The problem with this is that you have to ensure that your arrays are the same size.

    I would look into using a HashMap. You can get an enumeration object from them and then use the value and key parameters to get the data that you need.

  3. #3
    bluekswing is offline Member
    Join Date
    Jun 2007
    Posts
    14
    Rep Power
    0

    Default

    Thanks for the response! I ended up just doing the following:

    row = 5;
    col = 10;
    rowCount=0;

    while (row > 0) {

    String[][] array = new String[row][col];
    String[] array1 = new String[col];

    for (int i = rowCount; i<=rowCount; i++) {
    for (int j = 0; j<array[i].length; j++) {

    array1[j] = array[i][j];

    } //end of inner for loop

    } //end of outer for loop

    //instantiate object - pass array1 into the constuctor
    //build object
    //do stuff with object


    rowCount++;
    row--;
    } //end of while
    Last edited by bluekswing; 01-15-2008 at 06:13 PM. Reason: fixed code

Similar Threads

  1. Array Help
    By bluegreen7hi in forum New To Java
    Replies: 2
    Last Post: 03-28-2008, 02:25 AM
  2. can anyone help... 2d Array
    By Mark1989 in forum New To Java
    Replies: 2
    Last Post: 03-12-2008, 08:59 PM
  3. Would appreciate your help with 2d Array..
    By cloudkicker in forum New To Java
    Replies: 1
    Last Post: 02-11-2008, 02:34 PM
  4. Bounded Array
    By bugger in forum New To Java
    Replies: 4
    Last Post: 01-04-2008, 09:41 AM
  5. Help with Array
    By susan in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 04:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •