List<Integer> contains
时间: 2023-12-14 20:03:47 浏览: 79
Sure! If you're looking for the `List` class in Java that can contain integers, you can use the `java.util.List` interface and its implementation classes such as `java.util.ArrayList` or `java.util.LinkedList`. Here's an example:
```java
import java.util.List;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
List<Integer> numberList = new ArrayList<>();
// Adding integers to the list
numberList.add(10);
numberList.add(20);
numberList.add(30);
// Checking if the list contains a specific value
boolean containsValue = numberList.contains(20);
System.out.println("Does the list contain 20? " + containsValue);
}
}
```
In this example, we create an `ArrayList` called `numberList` and add some integers to it. Then, we use the `contains()` method to check if the list contains the value `20`. The result will be printed as `true` or `false` depending on whether the value is found in the list.
阅读全文