• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I pas arrays from one object to another?  RSS feed

 
Nicholas Seamans
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a little rusty with programming. What I want to do is access an array passed from one object to another. I have included source code of my failed efforts if anyone is curious in helping
I am trying to pass the 2d "board" array. Thank you
import java.util.*;


 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Please UseCodeTags (← click) when posting code as it makes it easier for people to read your code. I've added them for you this time.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the problem? It looks like you are correctly passing the array. In order to print the array's index values, try this:

 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Being slightly pedantic Java doesn't have 2D arrays, it has arrays of arrays.

BTW why is the board field static?

You are passing a reference to the array of arrays using the getBoard() method. But printing out an array of arrays can't be done by just printing the reference, you need to use a nested loop to iterate over the two dimensions and print each element or you could have a single loop and use the Array.toString(..) method to print an array at a time.
 
Nicholas Seamans
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Being slightly pedantic Java doesn't have 2D arrays, it has arrays of arrays.

BTW why is the board field static?

You are passing a reference to the array of arrays using the getBoard() method. But printing out an array of arrays can't be done by just printing the reference, you need to use a nested loop to iterate over the two dimensions and print each element or you could have a single loop and use the Array.toString(..) method to print an array at a time.


I do not know why I had the field static...........

Also not to request to much, but being able to access the information was my ultimate goal. Could you possibly show me how to do the first nested loop print out in the context of the controller class? I a learning java on my own accord and sometimes tutorials can be scatter-shot to say the least. :/
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel Christophel wrote:What's the problem? It looks like you are correctly passing the array. In order to print the array's index values, try this:

Joel please don't just the post the code in the Beginning Java forum, the idea here is to guide the OP so they can write the code themselves and learn from the experience.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicholas Seamans wrote:Could you possibly show me how to do the first nested loop print out in the context of the controller class?

Do you know what a nested loop is?
 
Nicholas Seamans
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
Nicholas Seamans wrote:Could you possibly show me how to do the first nested loop print out in the context of the controller class?

Do you know what a nested loop is?


Yes I got it now.

I guess the biggest problem now is that I keep only getting 0 as the output as if the random generator code is not working.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It shouldn't be full of zero's. Try adding a print statement in the loop where you fill the array to see what values you are putting into it.
This is wrong as the method is never called - see next post.
Although adding the print statement would show that the method isn't called so it's not totally wrong (I'm grasping at straws here )
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scrub my last reply. It will be full of zeros as you haven't called the method that fills it with random numbers.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
Joel please don't just the post the code in the Beginning Java forum, the idea here is to guide the OP so they can write the code themselves and learn from the experience.

I usually try to abstain from posting code, but since the code I posted wasn't relevant to his actual question, I thought it was okay.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!