# How to check if an int array contains a number

**URL:** <https://discourse.processing.org/t/how-to-check-if-an-int-array-contains-a-number/18068>\
**Category:** Beginners\
**Created:** [February 24, 2020, 11:12am UTC](https://discourse.processing.org/t/how-to-check-if-an-int-array-contains-a-number/18068 "2020-02-24T11:12:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yannik](https://avatars.discourse-cdn.com/v4/letter/y/5fc32e/32.png) [@Yannik](https://discourse.processing.org/u/Yannik)\
**Post date:** [February 24, 2020, 11:12am UTC](https://discourse.processing.org/t/how-to-check-if-an-int-array-contains-a-number/18068/1 "2020-02-24T11:12:57Z")

</div>

How can I check if an array contains a number? Here’s an example:

int [] array = new int [200];  
array[0] = int(random(0,200));  
If(array.contains 20){  
println(“20”);  
}

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [February 24, 2020, 11:41am UTC](https://discourse.processing.org/t/how-to-check-if-an-int-array-contains-a-number/18068/2 "2020-02-24T11:41:06Z")

</div>

Look through the Processing resources:  
[https://processing.org/](https://processing.org/)  
[https://processing.org/reference/](https://processing.org/reference/)  
[https://processing.org/tutorials/arrays/](https://processing.org/tutorials/arrays/)  
[https://processing.org/reference/Array.html](https://processing.org/reference/Array.html)  
[https://processing.org/reference/for.html](https://processing.org/reference/for.html)  
[https://processing.org/reference/if.html](https://processing.org/reference/if.html)

This is for a simple array…

At present the first two lines are assigning a random number to the first element array[0] of the array.

Once you start to understand arrays and work with them then:

- add a few line of code to loop through the array and display contents
- in the loop check to see if it contains the number.
- use println() statements strategically to assist with understanding code

Once you can work through this and have a working example there are more advanced ways to do this.

`:)`

---

<div class="post-metadata">

**Author:** ![anon94203042](https://avatars.discourse-cdn.com/v4/letter/a/898d66/32.png) [@anon94203042](https://discourse.processing.org/u/anon94203042)\
**Post date:** [February 25, 2020, 3:48am UTC](https://discourse.processing.org/t/how-to-check-if-an-int-array-contains-a-number/18068/3 "2020-02-25T03:48:51Z")

</div>

Nice question. So what we have to do here, is to go through all the numbers of the array, and check each one to see if they are equal to 20. We can do this with a loop.

```auto
for (int a = 0; a < ARRAY_SIZE; ++a)
{
   if (array[a] == 20)
      println("20");
}

```

So to visualize it:

```auto
array = { 7, 13, 20, 2 };

is the first element equal to 20?
if no, then is the second element equal to 20?
we keep doing this until an element hits 20

```

Hope this helps!

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [February 26, 2020, 7:14pm UTC](https://discourse.processing.org/t/how-to-check-if-an-int-array-contains-a-number/18068/4 "2020-02-26T19:14:21Z")

</div>

For a **primitive arrays** you must loop through all the values and return `true` if they are equal.

```auto
boolean contains(int[] arr, int val) {
  for(int i=0; i<arr.length; i++) {
    if(arr[i]==val) {
      return true;
    }
  }
  return false;
}

```

However, for **Object arrays** you can use “.contains()” or a related method. Your example is a list of ints, and Processing already has this built-in: `IntList.hasValue()`

[https://processing.org/reference/IntList\_hasValue\_.html](https://processing.org/reference/IntList_hasValue_.html)

If you are working with Strings or floats there is StringList and FloatList. For other objects there is ArrayList, et cetera.
