## How to grab only a certain row of a two-dimensional matrix in java?

Question

When I have a two-dimensional matrix in java, but I only want to work with a certain row of that matrix respectively creating a new array that contains the contents of the matrix in that certain row, how would I accomplish that (with primitives)?

So, for example, we would write:

```
int[][] matrix = new int[10][10];
```

Now we have a two-dimensional matrix with 10 rows and 10 columns. Assume that we fill the whole matrix with certain elements, and now, I wish to work only with the first row, meaning to define a new array that contains exactly the elements of the first row of the matrix.

Show source

## Answers ( 1 )

Assuming that row means the horizontal segments of the matrix (as it almost always is):

In a 2 dimensional array in java, the rows are the first index and the columns are the second index.

Basically a two dimensional array is an array of arrays. So

is actually an array of size 10. Each element in the array is

an array in itselfof size 3.Say you have an array of integers

then we want to work with the 1st row. We would use:

LIMITATIONSNote: we use

`integerArray[0]`

because arrays start at index 0, so the third row would be`integerArray[2]`