Results 1 to 4 of 4
- 04-12-2012, 11:40 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 10
- Rep Power
- 0
What won't this simple code work?
By doing the above, I'm trying to randomly place TypeA and TypeB objects (which both extend Organism) in city[ROWS][COLS].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; } }
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?")
- 04-12-2012, 12:33 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,406
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-12-2012, 12:35 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 04-12-2012, 02:54 PM #4
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Trying to create simple Timer, doesn't work
By Ragoune in forum New To JavaReplies: 4Last Post: 07-06-2011, 09:24 PM -
How does this very simple code work?
By Codeless in forum New To JavaReplies: 1Last Post: 11-29-2010, 11:43 PM -
Simple file reader won't work in eclipse
By BoomPony in forum New To JavaReplies: 3Last Post: 11-27-2010, 05:16 PM -
My Simple Array Does Not Work!
By Simplev_v in forum New To JavaReplies: 16Last Post: 09-07-2009, 02:43 PM -
Simple animation won't work
By nolsen01 in forum New To JavaReplies: 4Last Post: 07-08-2009, 11:36 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks