Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Drumstick
Jun 20, 2006
Lord of cacti
Im having a problem with 2d arrays. How can I assign a value to each spot in the array?
I dont know if this would further complicate the issue, but the size of the array is based on user input.

this is what i have right now. My hope is that this will assign a random boolean to each location within the array. Will this function how I would like it to?

quote:

boolean[][] lifegame = new boolean[i][h];

for( int a = 0; a < i; a++ )
for (int b = 0; b < h; b++){

Random random = new Random();
boolean r = random.nextBoolean();

lifegame[i][h]= r ;

Adbot
ADBOT LOVES YOU

Drumstick
Jun 20, 2006
Lord of cacti
yay, thanks for that catch.

I have one more problem im running into. How can i check the values next to each in an array?

say, if this would be how it is saved in the array

quote:


true |true |false
false|true |true
false|false|false



I need to be able to check the value for each adjacent item in the array. Is this possible, and any hint on how to do it?

Drumstick
Jun 20, 2006
Lord of cacti

Incoherence posted:

Suppose the point in question is lifegame[x][y]. What are the indices of the four adjacent points (or eight, if you want to go that way)?

Your question makes me unsure whether you understand what's going on here, so I'm being intentionally general to make sure we're not doing your homework for you or something. If you're having trouble, draw a picture of one of your grids and label each row and column with an index, then try and generalize to the question I asked above.

There's one catch: you'll want to check that each of your four/eight adjacent points are inside the matrix, but what you want to do about that will depend on whether you want to wrap around on edges or just drop them.

if i understand what your asking, you want the positions of the 8 adjacent 'boxes'

quote:


[x-1][y-1] [x-1][y] [x-1][y+1]
[x][y-1] [x][y] [x][y+1]
[x+1][y-1] [x+1][y] [x+1][y+1]


Im assuming this is what you meant by that. As for checking, they will need to drop off and not wrap around. Let me know if i understood what you were asking.

Drumstick
Jun 20, 2006
Lord of cacti
haha, yeah thanks for helping point my mind in the right direction. as soon as i drew it out and worked it out I saw a possible way to do it. Thank you so much.

Drumstick
Jun 20, 2006
Lord of cacti
ahhhh! quick question

How can I add 2 things into the same JFrame!

Both will work seperately, but when I try and add both in at the same time one will overwrite the other. im hoping this will be something somewhat simple.

quote:


JFrame frame = new JFrame();
frame.add(component);
frame.add(component2);


Component2 will show, but component wont show up at all. By themeselves, they will be up and working just fine, just not together...

Drumstick fucked around with this message at 06:59 on Apr 25, 2008

Adbot
ADBOT LOVES YOU

Drumstick
Jun 20, 2006
Lord of cacti
okay thanks! i will look into that right away. I havent had to add two components into a frame before, thanks again

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply