# Find the number of elements in a single row

**URL:** <https://discourse.processing.org/t/find-the-number-of-elements-in-a-single-row/28487>\
**Category:** Coding Questions\
**Created:** [March 13, 2021, 7:36am UTC](https://discourse.processing.org/t/find-the-number-of-elements-in-a-single-row/28487 "2021-03-13T07:36:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Criste44](https://avatars.discourse-cdn.com/v4/letter/c/a8b319/32.png) [@Criste44](https://discourse.processing.org/u/Criste44)\
**Post date:** [March 13, 2021, 7:36am UTC](https://discourse.processing.org/t/find-the-number-of-elements-in-a-single-row/28487/1 "2021-03-13T07:36:22Z")

</div>

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] …

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 13, 2021, 9:08am UTC](https://discourse.processing.org/t/find-the-number-of-elements-in-a-single-row/28487/2 "2021-03-13T09:08:24Z")

</div>

Make a for loop with i for example

That’s your x

The y is the line number

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

I think I did that for you recently

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 13, 2021, 9:10am UTC](https://discourse.processing.org/t/find-the-number-of-elements-in-a-single-row/28487/3 "2021-03-13T09:10:25Z")

</div>

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

Maybe make a function for this

2 is the line number

---

<div class="post-metadata">

**Author:** ![Criste44](https://avatars.discourse-cdn.com/v4/letter/c/a8b319/32.png) [@Criste44](https://discourse.processing.org/u/Criste44)\
**Post date:** [March 13, 2021, 12:56pm UTC](https://discourse.processing.org/t/find-the-number-of-elements-in-a-single-row/28487/4 "2021-03-13T12:56:41Z")

</div>

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++;

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [March 13, 2021, 1:15pm UTC](https://discourse.processing.org/t/find-the-number-of-elements-in-a-single-row/28487/5 "2021-03-13T13:15:43Z")

</div>

Hi @Criste44,

Consider this example:

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

```

Output:

```
4
```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [March 13, 2021, 1:46pm UTC](https://discourse.processing.org/t/find-the-number-of-elements-in-a-single-row/28487/6 "2021-03-13T13:46:06Z")

</div>

@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

```auto
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

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [March 13, 2021, 1:58pm UTC](https://discourse.processing.org/t/find-the-number-of-elements-in-a-single-row/28487/7 "2021-03-13T13:58:39Z")

</div>

Hi @Criste44 ,

Evidently, I misunderstood your intent when writing [this](https://discourse.processing.org/t/find-the-number-of-elements-in-a-single-row/28487/5). 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.

Instead, consider @Chrisir 's advice.

---

<div class="post-metadata">

**Author:** ![Criste44](https://avatars.discourse-cdn.com/v4/letter/c/a8b319/32.png) [@Criste44](https://discourse.processing.org/u/Criste44)\
**Post date:** [March 13, 2021, 4:50pm UTC](https://discourse.processing.org/t/find-the-number-of-elements-in-a-single-row/28487/8 "2021-03-13T16:50:38Z")

</div>

Unfortunately it was not what I wanted to do in my project, but thanks for your explanatory help.

Edit; now i fixed the problem 😊
