Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: How to get individual int values out of int[][]

Tags:

java

int

extract

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?

like image 875
Dillon Chaffey Avatar asked Dec 24 '11 14:12

Dillon Chaffey


People also ask

Can be int from integer in Java?

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.


1 Answers

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];
like image 185
ziesemer Avatar answered Oct 01 '22 17:10

ziesemer