Answers for "write a java program to test if an array contains a specific value."

6

Check if an array contains an element java

import java.util.Arrays;

// For String
String[] array = {"Boto", "Nesto", "Lepta"};
String toSearch = "Nesto";

// Inline
if (Arrays.toString(array).contains(toSearch)) {
	// Do something if it's found
}

// Multi line
String strArray = Array.toString(array);
if (strArray.contains(toSearch)) {
	// Do your thing
}

// Different elements
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
int number = 5;

String checker = Arrays.toString(numbers);

// The toString of int in some cases can happen without explicitly saying so
// In this example we convert both
if (checker.contains(Integer.toString(number)) {
	// Found.     
}
Posted by: Guest on January-14-2021
0

check if array contains a number in java

public static boolean contains(final int[] arr, final int key) {
    return Arrays.stream(arr).anyMatch(i -> i == key);
}
Posted by: Guest on May-24-2021

Code answers related to "write a java program to test if an array contains a specific value."

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language