1

I have an array like this:

0  1  2
3  4  5
6  7  8

these numbers are indices of this array

this array has been initialized with:

1,1  3,1  5,1
1,3  3,3  5,3
1,5  3,5  5,5

which these numbers are the coordinate of the grid(the value inside my array). so my question is that, what should i do if i want to retrieve the value of one cell by it's index, for example if i call a method like getValue(5); the result would be 5,3. i know how to get the value in multi dimentional array but i don't know how to get taht with it's index.

Thank you

1 Answer 1

1

yourArray[5] gives a one dimensional array containing 5 and 3.

Edit: To address your comment.

function customIndex(int i) {
    return myArray[Math.floor(i/3)][i%3];
}
Sign up to request clarification or add additional context in comments.

2 Comments

hmmmm but i don't have 5 rows right? i mean by calling myArray[5] i think the one dimensional array in row 5 will return, please correct me if i'm wrong. i think myArray[1][2] should give me 5,3 but i want just 1 int to call them like myArray[5].
You said that those numbers were the indices of your array. In the situation you're describing, you just need a function like function customIndex(int i) { return myArray[Math.floor(i/3)][i%3]; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.