Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Question regarding 2d arrays

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Question regarding 2d arrays

    Ok, this may seem like a dumb question, but I am doing a creative project for my AP computer science class, and I chose to do an RPG(original, huh? lol) anyway, it is text based and the way I have it is that you start off at a certain point on a grid (using 2d) array, and you go north south east and west.

    Now, the problem is I need to know if there is a way to store different methods I guess, in the individual spaces in the 2d array. Is that even possible? I just want to be able to use this for each event that the player would encounter. Anyway, if this is not possible, can you tell me of a different way to do this?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Question regarding 2d arrays

    Yes it's possible to via reflection, but the better way would probably be to create an interface and have each tile hold an object which implements that interface.

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question regarding 2d arrays

    I am new to java, so bear with me, how exactly would you be able to have each tile hold an object that implements the interface? What would the code for that look like? Would it simply be for me to set a variable equal to the method? Or how would you do that? I am not completely sure what it would look like.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Question regarding 2d arrays

    Methods do NOT have an existence outside of a class. You can't move them around, assign them, store them etc. You do that with a class object.
    As helloworld922 said, you create a class with the method.
    Then you can create instances of that class and treat a reference to that instance like any other variable.
    You can create an array that holds instances of your class. For example:
    YourClass[] tableOfItems = new YourClass[13]; // create array to hold 13 YourClass objects

  5. #5
    Junior Member
    Join Date
    May 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question regarding 2d arrays

    Yes I knew that, but what I was confused on was how to store the instances of that class into an individual cell, that is what I need help on. Like I wanted to make 2darray[0][0] = some call to a method, or something. That is what I am trying to do. When they move down they will go to 2darray[1][0] in which would have a new method saying "You traveled south, blah blah, blah".

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Question regarding 2d arrays

    how to store the instances of that class into an individual cell
    Just like with any data type:
    tableOfItems[3] = new YourClass(); // store an instance of YourClass at index 3

    I wanted to make 2darray[0][0] = some call to a method
    Say the class YourClass has a method: methodInYourClass()

    To call the method:
    tableOfItems[3].methodInYourClass(); // call a method on instance at index 3

  7. #7
    Junior Member
    Join Date
    May 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question regarding 2d arrays

    Ok, so what I would have to do is to store an instance of my class in each cell like at 0,0 all the way to 10,10 if I had a 10 X 10 grid. Correct?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Question regarding 2d arrays

    Yes, well almost. The max index in a 10x10 array would be 9,9

  9. #9
    Junior Member
    Join Date
    May 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question regarding 2d arrays

    Yeah, alright thanks guys for your help!

Similar Threads

  1. Question about arrays and jtexfields
    By CheekySpoon in forum Java Theory & Questions
    Replies: 2
    Last Post: May 5th, 2011, 04:32 PM
  2. Question on arrays.
    By SV25 in forum Java Theory & Questions
    Replies: 4
    Last Post: April 24th, 2011, 02:45 AM
  3. 2d arrays help
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 27th, 2010, 04:27 PM
  4. Arrays
    By mlan in forum Java Theory & Questions
    Replies: 2
    Last Post: February 26th, 2010, 09:23 AM
  5. Arrays: question about "removing" items.
    By sanfor in forum Collections and Generics
    Replies: 2
    Last Post: November 30th, 2009, 03:09 AM