The List
interface defines the method contains
. Think of an interface as a contract that classes can "sign" (in Java this is done with the keyword implements
) which says that the class must have certain things in its implementation. A very common implementation of the List
interface is ArrayList
, but Lists do not work very well with the primitive int type, so what you want to do is make an ArrayList
of Integers.
The simplest way to make an ArrayList of Integers is to make an array of Integers first (I know, Java has a lot of weird steps required to get things working).
In addition, you want to make sure that boolean methods always return a boolean value or you will get a compiler error.
Here's a working example:
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class LotteryTicket {
private String nameOfBuyer;
private List<Integer> numberList;
private boolean search(int val) {
return (val >= 1 && val <=50) && numberList.contains(val);
}
public static void main(String[] args) {
LotteryTicket lt = new LotteryTicket();
Integer[] numberList = new Integer[] {2, 3, 4, 5, 42, 6};
lt.numberList = new ArrayList<Integer>(Arrays.asList(numberList));
System.out.println(lt.search(42)); // prints "true\n"
System.out.println(lt.search(25)); // prints "false\n"
}
}