# Find the number of elements in a single row

hello … [8, 8] I have a two dimensional array, while the game is going on I want to find out how many objects are in a single line … not the whole array, just one line, example [i, 7] …

Make a for loop with i for example

The y is the line number

Then make a counter and say ++ when cell is filled

I think I did that for you recently

count2=0;
for (int x=0; x < 8; x++) {
if (grid[x].selected)
count2++;
}//for
//

Maybe make a function for this

2 is the line number

I did something like this

int A = 0;

for (int i = 0; i < 8; i++)
{
for(int j = 0; j < 8; j++)
{
if(piece[i, 7] != null) {
A++;

Hi @Criste44,

Consider this example:

``````void setup() {
int nums[][] = {{3, 4, 7}, {1, 9, 8, 6}, {8, 1, 2}};
System.out.println(nums.length);
}
``````

Output:

``4``

@Criste44

consider to post a running code where we can test this

when I last answered you (when you asked almost the same question in another discussion) I posted a running code with a grid. It is very useful because then we can run and test it.

Now above you presented a nested for-loop which is not logical, because you want to check the fields in one line, not in all of them (because of this the variable `j` is not used)

here is my example, you can pass the line number to the function and it returns the count for this line

``````int countLine ( int whichLine ) {

int count=0;

for (int x=0; x < 8; x++) {
if (grid[x][whichLine].selected) {
count++;
}
}//for

return count;
}//func
//
``````

you can test it with `text( countLine (3) , 100, 100);`

it works for horizontal lines, not vertical lines

I hope this helps!

Warm regards,

Chrisir

1 Like

Hi @Criste44 ,

Evidently, I misunderstood your intent when writing this. I had incorrectly assumed you were using a jagged array wherein the size of the rows would be changing during the course of the game.

Edit; now i fixed the problem 