# Thread: Back to the math class....

1. Senior Member
Join Date
May 2012
Posts
172
Rep Power
2

## 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 01:34 AM.

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

## 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. Senior Member
Join Date
May 2012
Posts
172
Rep Power
2

## 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?

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

## 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. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## 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?

6. Senior Member
Join Date
May 2012
Posts
172
Rep Power
2

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

Originally Posted by Tolls
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.

7. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## 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.

8. Senior Member
Join Date
May 2012
Posts
172
Rep Power
2

## 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

9. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## 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.

10. ## 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

11. Senior Member
Join Date
May 2012
Posts
172
Rep Power
2

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

Originally Posted by JosAH
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)

12. Senior Member
Join Date
May 2012
Posts
172
Rep Power
2

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

Ok guys I've figured the formula on my own, thanks for trying to help me!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•