Created
May 17, 2021 15:27
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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