Function for ArrayList (ArrayList as parameter)

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:

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?

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:

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

Can’t do it quite that way

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

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

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

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

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

/**
 * 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;
}
1 Like

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”

1 Like
  • 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.

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