Skip to content

Instantly share code, notes, and snippets.

@gauravkukade
Created May 17, 2021 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gauravkukade/7cf0ba43baccbc9e828277c19f5c14a7 to your computer and use it in GitHub Desktop.
Save gauravkukade/7cf0ba43baccbc9e828277c19f5c14a7 to your computer and use it in GitHub Desktop.
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