public class Example {
public static void main(String... args) {
String[] myArray = new String[6];
myArray[0] = "USA";
myArray[1] = "France";
myArray[2] = "Germany";
myArray[3] = "Australia";
myArray[4] = "Japan";
myArray[5] = "India";
System.out.println(checkArrayForElement(myArray,"Japan"));
System.out.println(checkArrayForElement(myArray,"China"));
}
public static boolean checkArrayForElement(String[] myArray, String country) {
for (int i = 0; i < myArray.length; i++) {
if(myArray[i].equals(country)) {
return true;
}
}
return false;
}
}
Output:
true
false
public class Example {
public static void main(String... args) {
int[] myIntArray = {1, 4, 0, 11, 22, 34, 24, 12};
System.out.println(checkArrayForElement(myIntArray,12));
System.out.println(checkArrayForElement(myIntArray,10));
}
public static boolean checkArrayForElement(int myIntArray, int inNo) {
for (int no: myIntArray) {
if(no == inNo) {
return true;
}
}
return false;
}
}
Output:
true
false
In Example 1, we have an array of Strings and we iterate over the array's length and check with an if the condition that the array contains an element or not using equals() methods. In Example 2 we are making use of for each loop to do the same with an array of integers.
More Posts related to Java,
- Drop table using Java JDBC Template
- Java - Check if array contains the value
- YAML Parser using Java Jackson Library Example
- Java Jackson ObjectMapper Class with Examples
- Get Client IP address from HTTP Response in Java
- How to Word-Warp Console logs in IntelliJ
- Exception in thread main java.lang.NoClassDefFoundError: package javaClass
- Setting Java_Home Environment variable on Windows Operating System
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Java SE JDBC Select Statement Example
- How to extract Java Jar/War/Ear files in Linux
- java: unclosed string literal [Error]
- [Solution] Exception in thread main java.util.EmptyStackException
- Read YAML file Java Jackson Library
- What Java version is used for Minecraft 1.18
- [Java] How to throws Exception using Functional Interface and Lambda code
- [Program] How to read three different values using Scanner in Java
- Java 8 Predicate Functional Interface Examples
- Display Era details (AD BC) in Java Date using SimpleDateFormat
- Convert String Date to Date Object in Java
- Struts 2 Hello World Example in Eclipse
- Read a file using Java 8 Stream
- Java - How to set custom thread name?
- IntelliJ: Error: Could not find or load main class, java.lang.ClassNotFoundException
- java: ']' expected error [fixed]
More Posts:
- Splitting String in Java with Examples - Java
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ - Java
- How to remove/delete a directory in Linux/macOs - Linux
- Fix: Cannot contact reCAPTCHA. Check your connection and try again. - Google
- Android Constant and Resource Type Mismatches Lint - Android
- Microsoft Teams - Where would you like to start - Business or Personal - Teams
- How to add Date and Time to Windows Notepad File - NotepadPlusPlus
- [Fix] java: incompatible types: incompatible parameter types in lambda expression error - Java