I have an int[][] object. It is defined in my code as below:
public int[][] position = {
{20, 30}, {73, 91},
{82, 38}
};
Would it be possible to get the value of the first value (on the left) within each of the pairs of parentheses and store them as individual int variables using a for loop? Basically, is it possible to extract the "20", "73" and "82" and store them into int variables individually?
In Java, int is a primitive data type while Integer is a Wrapper class. int, being a primitive data type has got less flexibility. We can only store the binary value of an integer in it. Since Integer is a wrapper class for int data type, it gives us more flexibility in storing, converting and manipulating an int data.
for(int[] x : position){
int y = x[0];
// Do something with y...
}
Or just:
int x = position[0][0];
int y = position[1][0];
int y = position[2][0];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With