1import java.util.Arrays;
2
3// For String
4String[] array = {"Boto", "Nesto", "Lepta"};
5String toSearch = "Nesto";
6
7// Inline
8if (Arrays.toString(array).contains(toSearch)) {
9 // Do something if it's found
10}
11
12// Multi line
13String strArray = Array.toString(array);
14if (strArray.contains(toSearch)) {
15 // Do your thing
16}
17
18// Different elements
19int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
20int number = 5;
21
22String checker = Arrays.toString(numbers);
23
24// The toString of int in some cases can happen without explicitly saying so
25// In this example we convert both
26if (checker.contains(Integer.toString(number)) {
27 // Found.
28}
29
1package com.mkyong.core;
2
3import java.util.Arrays;
4import java.util.List;
5
6public class StringArrayExample1 {
7
8 public static void main(String[] args) {
9
10 String[] alphabet = new String[]{"A", "B", "C"};
11
12 // Convert String Array to List
13 List<String> list = Arrays.asList(alphabet);
14
15 if(list.contains("A")){
16 System.out.println("Hello A");
17 }
18
19 }
20
21}
22Copy
1 // Convert to stream and test it
2 boolean result = Arrays.stream(alphabet).anyMatch("A"::equals);
3 if (result) {
4 System.out.println("Hello A");
5 }
6Copy