# Function for ArrayList (ArrayList as parameter)

**URL:** https://discourse.processing.org/t/function-for-arraylist-arraylist-as-parameter/27759
**Category:** Coding Questions
**Created:** [February 12, 2021, 2:29pm UTC](https://discourse.processing.org/t/function-for-arraylist-arraylist-as-parameter/27759 "2021-02-12T14:29:47Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![jensemand](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@jensemand](https://discourse.processing.org/u/jensemand)
#### Post date: [February 12, 2021, 2:29pm UTC](https://discourse.processing.org/t/function-for-arraylist-arraylist-as-parameter/27759/1 "2021-02-12T14:29:47Z")

</div>

Hi, I am trying to make a function that takes an arraylist and an object as input and checks if the object is in the arraylist. My idea is something like:

```auto
boolean includes(Cell[] grid2, Cell tjek){
  for (int i=0;i<grid2.size(); i++){
    if (grid2.get(i)==tjek){
      return true;
    }
  }
}

```

However this doesn’t seem to work. Is there a function i can use for checking an arraylist or how can i make a function to check if an object is in the arraylist?

---

<div class="post-metadata">

### Author: ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)
#### Post date: [February 12, 2021, 2:37pm UTC](https://discourse.processing.org/t/function-for-arraylist-arraylist-as-parameter/27759/2 "2021-02-12T14:37:02Z")

</div>

Hello! The problem with your boolean method is that there is a possibility that it doesn’t return a value at all, and boolean methods always have to return a value. Also, you are stating the cell grid as an array, not an arraylist. Try this:

```auto
boolean includes(ArrayList<Cell> grid2, Cell x){
  for (int i=0;i<grid2.size(); i++){
    if (grid2.get(i)==x){
      return true;
    } else {
return false;
}
  }
}

```

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [February 12, 2021, 2:40pm UTC](https://discourse.processing.org/t/function-for-arraylist-arraylist-as-parameter/27759/3 "2021-02-12T14:40:57Z")

</div>

Can’t do it quite that way

```auto
boolean includes(ArrayList<Cell> grid2, Cell x){
  for (int i=0;i<grid2.size(); i++){
    if (grid2.get(i)==x){
      return true;
    }
  }
  return false;
 }

```

Alternatively

```auto
boolean includes(ArrayList<Cell> grid2, Cell x){
    return grid2.contains(x);
}

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [February 12, 2021, 8:15pm UTC](https://discourse.processing.org/t/function-for-arraylist-arraylist-as-parameter/27759/4 "2021-02-12T20:15:43Z")

</div>

> [@jensemand](#):
>
> Is there a function I can use for checking an ArrayList…

[docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collection.html#contains(java.lang.Object)](http://docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/util/Collection.html#contains(java.lang.Object))

> [@jensemand](#):
>
> … or how can I make a function to check if an object is in the ArrayList?

Although you’re asking specifically for ArrayList, your attempt at coding an **includes()** function requests a plain array of datatype Cell[] instead:

> [@jensemand](#):
>
> `boolean includes(Cell[] grid2, Cell tjek) {`

As I’m not so sure you want an array or an ArrayList solution I did both container types:

```auto
/**
 * Container Includes (v1.0.4)
 * GoToLoop (2021/Jan/12)
 * https://Discourse.Processing.org/t/function-for-arraylist/27759/4
 */

final PVector[] vectorArray = {
  new PVector(), 
  new PVector(PI, TAU), 
};

final ArrayList<PVector> vectorList = new ArrayList<PVector>(
  java.util.Arrays.asList(vectorArray));

void setup() {
  println(vectorArray);
  println(vectorList);
  println();

  println(includes(new PVector(), vectorArray)); // true
  println(includes(new PVector(PI, TAU), vectorArray)); // true
  println(includes(new PVector(TAU, PI), vectorArray)); // false
  println(includes(null, vectorArray)); // false
  println();

  println(includes(new PVector(), vectorList)); // true
  println(includes(new PVector(PI, TAU), vectorList)); // true
  println(includes(new PVector(TAU, PI), vectorList)); // false
  println(includes(null, vectorList)); // false
  println();

  vectorList.add(null);
  println(includes(null, vectorList)); // true

  exit();
}

@SafeVarargs static final <T> boolean includes(final T elem, final T... arr) {
  if (arr != null && arr.length > 0) if (elem != null) {
    for (final T item : arr) if (elem.equals(item)) return true;
  } else for (final T item : arr) if (item == null) return true;

  return false;
}

static final <T> boolean includes(final T e, final java.util.Collection<T> c) {
  return c != null? c.contains(e) : false;
}

```

---

<div class="post-metadata">

### Author: ![jensemand](https://avatars.discourse-cdn.com/v4/letter/j/b38774/32.png) [@jensemand](https://discourse.processing.org/u/jensemand)
#### Post date: [February 13, 2021, 5:29pm UTC](https://discourse.processing.org/t/function-for-arraylist-arraylist-as-parameter/27759/5 "2021-02-13T17:29:44Z")

</div>

This doesn’t seem to work. it just says the method must return a result of type boolean, but it doesn’t make sense besause it says “return true” or “return false”

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [February 13, 2021, 6:41pm UTC](https://discourse.processing.org/t/function-for-arraylist-arraylist-as-parameter/27759/6 "2021-02-13T18:41:24Z")

</div>

> [@jensemand](#):
>
> , but it doesn’t make sense besause it says “return true” or “return false”

- Those 2 `return` statements are inside a `for ( ; ; )` loop block.
- And in order to enter that loop the condition `; i < grid2.size();` must be satisfied.
- Therefore there’s a chance that function may not `return` a `boolean` after all.

---

<div class="post-metadata">

### Author: ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)
#### Post date: [February 13, 2021, 8:10pm UTC](https://discourse.processing.org/t/function-for-arraylist-arraylist-as-parameter/27759/7 "2021-02-13T20:10:17Z")

</div>

Anyways, quark’s method should work if mine doesn’t. I think that that contains() method would work better.
