Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A Java program to check if a primitive array contains a primitive value. Check the blogpost at https://coderolls.com/java-check-if-array-contains-value-or-element/
/**
* A Java program to check if a primitive array contains a primitive value
*
* @author Gaurav Kukade at coderolls.com
*
*/
public class CheckInPrimitiveArray {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5, 6, 7};
int intToCheck = 5;
boolean contains = false;
for(int number: numbers) {
// check if number and intToCheck are equal,
// if yes make contains as true and break the loop
if(number == intToCheck) {
contains = true;
break;
}
}
if(contains){
System.out.println("The numbers array contians the value '"+intToCheck+"'.");
}else {
System.out.println("The numbers array does not contian the value '"+intToCheck+"'.");
}
}
//Output:
// The numbers array contians the value '5'.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment