Results 1 to 12 of 12
  1. #1
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Question Back to the math class....

    Hello.
    Basically I've made an inventory that has total spaces 2,4. 0 is a space too, so it goes like this
    0,0-1,0-2,0
    0,1-1,1-2,1
    and so on..
    Basically I have total spaces: 14 (0 is a space too)

    Now the question is, if I have to work with space 2,2 or 2,3, how do I get the number of this slot? (Without a for loop)
    Example:
    0,0-1,0-2,0
    0,1-1,1-2,1
    0,2-1,2-2,2
    the coordinate is 1,0 - it a slot number 1.
    the coordinate is 2,0 - it a slot number 2.
    the coordinate is 1,2 - it a slot number 8.
    and so on..
    Any formulas?

    EDIT:
    Ok even loops would be fine.
    EDIT 2:
    Never mind, NO LOOPS.
    Last edited by Lionlev; 10-31-2012 at 12:34 AM.
    WARNING I am Russian so it's possible that I wont understand you correctly...

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Back to the math class....

    If the integer pairs represent something write a class so you can work with it.

    Then, in a loop, (1) read the input, (2) create instances of the class and (3) add them to a list.

    When you want to find the index of a given pair within the list it will be a simple matter to use another loop to go through the list until you have an element with the appropriate coordinates.

  3. #3
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Back to the math class....

    I am using the loop right now, and it works, but is there a mathematical formula for that? so that would be like one line?
    WARNING I am Russian so it's possible that I wont understand you correctly...

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Back to the math class....

    I'm not exactly sure what you want the formula to do.

    Also, why no loops? If it's assignment problem it might be a good idea to post the exact wording so we can see what it means.

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

    Default Re: Back to the math class....

    If these slots represent something (ie the coordinates are meaningful) then build your inventory to match, so you can get the correct slot simply by providing the coordinates.
    Is this a multi-dimensional array?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Back to the math class....

    Quote Originally Posted by Tolls View Post
    If these slots represent something (ie the coordinates are meaningful) then build your inventory to match, so you can get the correct slot simply by providing the coordinates.
    Is this a multi-dimensional array?
    I've got few arraylists
    Point (the slot's position) (x, y)
    Rectangle (to make the slots click able)
    Integer (holds all the slots ids)
    Boolean (if the slot is empty)
    They way my inventory works is it creates slots and adding info to each arraylist and the boolean on each one is true
    Then I get the info from the server in format "id x y" I have to find that x and y in the arraylist and set boolean to false and set the Integer to the id that the server sent.
    WARNING I am Russian so it's possible that I wont understand you correctly...

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

    Default Re: Back to the math class....

    I'm sorry, but 'x' and 'y' imply a multi-dimensional array to me, so unless your ArrayList is a List<List<Data>> I would argue your model is wrong.
    Indeed, you seem to have a parallel array thing going on as well.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Back to the math class....

    Well it's not WRONG because it works just fine, but that is the probably long way to do it...

    So I didn't actually knew that it's possible to make an array that stores multiple arguments like "boolean, integer, x,y".. I should look that up
    WARNING I am Russian so it's possible that I wont understand you correctly...

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

    Default Re: Back to the math class....

    Well, you'd create a class that represents the data (that's a model).
    Then you'd have an ArrayList<YourClass>.

    But I wouldn't put the x and y in there.
    I really would consider a 2 dimensional array.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: Back to the math class....

    Can I just say that I don't understand one single syllable of this thread and I neither understand what mathematics have to do with it ...

    kind regards,

    Jo
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Back to the math class....

    Quote Originally Posted by JosAH View Post
    Can I just say that I don't understand one single syllable of this thread and I neither understand what mathematics have to do with it ...

    kind regards,

    Jo
    I have to find the formula for this question:
    The maximum of the coordinate in the table is (3;10)
    How many coordinates (a) do I have to pass to reach the given coordinate (2;6)?
    x=2
    y=6
    maxX=3
    maxY=10
    a = ?formula?
    (a should be 17, but what is the formula to get the answer? because the give coordinate can be different)
    WARNING I am Russian so it's possible that I wont understand you correctly...

  12. #12
    Lionlev is offline Senior Member
    Join Date
    May 2012
    Posts
    172
    Rep Power
    3

    Default Re: Back to the math class....

    Ok guys I've figured the formula on my own, thanks for trying to help me!
    WARNING I am Russian so it's possible that I wont understand you correctly...

Similar Threads

  1. Ceiling and Floor without using Math class
    By teardrop3903 in forum New To Java
    Replies: 1
    Last Post: 10-13-2011, 05:29 AM
  2. help with math class and random numbers
    By NestorPrime in forum New To Java
    Replies: 9
    Last Post: 10-01-2011, 09:57 PM
  3. .class to .java and back
    By bluerune in forum New To Java
    Replies: 8
    Last Post: 07-05-2010, 03:14 AM
  4. Math Class
    By ritwik07 in forum New To Java
    Replies: 2
    Last Post: 09-14-2009, 04:06 PM
  5. Math class question
    By shendel in forum New To Java
    Replies: 1
    Last Post: 03-05-2009, 12:06 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
  •