Results 1 to 4 of 4
  1. #1
    xpl0rerchr is offline Member
    Join Date
    Mar 2012
    Posts
    10
    Rep Power
    0

    Default What won't this simple code work?

    Java Code:
        public World(int numTypeA, int numTypeB) {
            city = new Organism[ROWS][COLS];
            rand = new Random();
            while (numTypeA > 0) {
                Coordinates temp = new Coordinates(rand.nextInt(ROWS), rand.nextInt(COLS));
                if (city[temp.row][temp.col] == null) {
                    city[temp.row][temp.col] = new TypeA(this, temp);
                    numTypeA--;
                }
            }
            while (numTypeB > 0) {
                Coordinates temp = new Coordinates(rand.nextInt(ROWS), rand.nextInt(COLS));
                if (city[temp.row][temp.col] == null) {
                    city[temp.row][temp.col] = new TypeB(this, temp);
                    numTypeB--;
                }
            }
        }
    
    public class Coordinates {
        int row;
        int col;
    
        Coordinates(int x, int y) {
            this.row = x;
            this.col = y;
        }
    
    }
    By doing the above, I'm trying to randomly place TypeA and TypeB objects (which both extend Organism) in city[ROWS][COLS].
    The problem is that apparently they are created at city[column][row] each time, instead of city[row][column]. Can anyone see why this would happen?

    (PS. I meant to say "Why won't this simple code work?")

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default Re: What won't this simple code work?

    Maybe there is confusion about what the row index is and what the column index is. A two dimensional array is just an array of one dimensional arrays. The first index denotes a one dimensional array and the second index is an index value for that one dimensional array. The first index is called the 'row index' while the second one is called the 'column index'. Two dimensional arrays are said to be in 'row major order'.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: What won't this simple code work?

    Stick some println() calls in there to see what's going on in that code.
    Debugging it is the only way you'll know what values Java is actually using, so print out the coordinates each time and then the city array (possibly) to see where everything's ended up.
    That code there seems to have row/column laid out the same everywhere, so I doubt it's that.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: What won't this simple code work?

    Have you abandoned your previous thread then?
    NullPointerException for no apparent reason?

    Common courtesy would dictate that you tell us there that your problem is solved.

    And I wouldn't construct a new Random object each time into the method, especially when it's already declared as a field.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Trying to create simple Timer, doesn't work
    By Ragoune in forum New To Java
    Replies: 4
    Last Post: 07-06-2011, 09:24 PM
  2. How does this very simple code work?
    By Codeless in forum New To Java
    Replies: 1
    Last Post: 11-29-2010, 11:43 PM
  3. Simple file reader won't work in eclipse
    By BoomPony in forum New To Java
    Replies: 3
    Last Post: 11-27-2010, 05:16 PM
  4. My Simple Array Does Not Work!
    By Simplev_v in forum New To Java
    Replies: 16
    Last Post: 09-07-2009, 02:43 PM
  5. Simple animation won't work
    By nolsen01 in forum New To Java
    Replies: 4
    Last Post: 07-08-2009, 11:36 PM

Posting Permissions

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