Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
The questions need to be more precise. Since nifiga is not clear how your classes are arranged. There is no such type T, this letter usually denotes a parameterized type.
If you just need to find out if an object exists or not, then no parameterization is required:
public class Foo {
static List<Object> list = new LinkedList<>();
public static boolean hasElem(Object elem) {
for (Object item : list) {
if (Objects.equals(item, elem)) {
return true;
}
}
return false;
}
public static void main(String[] args) {
list.add(1);
list.add("s1");
list.add(2);
list.add("s2");
list.add(3);
list.add("s3");
if (hasElem(3)) {
System.out.println("Has elem 3");
}
if (hasElem("s2")) {
System.out.println("Has elem \"s2\"");
}
if (hasElem('c')) {
System.out.println("Has elem 'c'");
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question