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
1import java.util.Scanner;
2public class Search_Element
3{
4 public static void main(String[] args)
5 {
6 int n, x, flag = 0, i = 0;
7 Scanner s = new Scanner(System.in);
8 System.out.print("Enter no. of elements you want in array:");
9 n = s.nextInt();
10 int a[] = new int[n];
11 System.out.println("Enter all the elements:");
12 for(i = 0; i < n; i++)
13 {
14 a[i] = s.nextInt();
15 }
16 System.out.print("Enter the element you want to find:");
17 x = s.nextInt();
18 for(i = 0; i < n; i++)
19 {
20 if(a[i] == x)
21 {
22 flag = 1;
23 break;
24 }
25 else
26 {
27 flag = 0;
28 }
29 }
30 if(flag == 1)
31 {
32 System.out.println("Element found at position:"+(i + 1));
33 }
34 else
35 {
36 System.out.println("Element not found");
37 }
38 }
39}
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
1public class Contains {
2
3 public static void main(String[] args) {
4 int[] num = {1, 2, 3, 4, 5};
5 int toFind = 3;
6 boolean found = false;
7
8 for (int n : num) {
9 if (n == toFind) {
10 found = true;
11 break;
12 }
13 }
14
15 if(found)
16 System.out.println(toFind + " is found.");
17 else
18 System.out.println(toFind + " is not found.");
19 }
20}