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,
- How to install Java 11 on Mac
- Get Client IP address from HTTP Response in Java
- SharePoint Open in the client application document opens in browser
- How to verify if java is installed on the computer and get version detail
- Java - Check if array contains the value
- Your JBoss Application Server 7 is running However you have not yet added any users to be able to access the admin console
- How to run Java Unit Test cases with Apache Maven?
- What Java version is used for Minecraft 1.18
- List of jar files for Jax-ws (SOAP) based Java Web Services
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- Convert Instant timestamp into LocalDateTime Java Code Example
- Java - Calculate time taken for the code to execute in milliseconds or nanoseconds
- Create simple struts2 project using maven commands
- Java - PatternSyntaxException
- List of Online Java compiler with console
- Minecraft Java Edition
- How to declare and initialize Array in Java Programming
- [Solved] com.sun.xml.ws.transport.http.servlet.WSServletContextListener ClassNotFoundException
- Java XML-RPC 3.1.x based web service example
- Simple Struts 2 Tutorial in eclipse with tomcat 7 server
- Java 8 foreach loop code examples
- Java -Day of the week using Java 8 DayOfWeek Enum
- Java 8 - Convert List to Map Examples
- Java: TimeZone List with GMT/UTC Offset
- list of jars required for hibernate 4.x.x
More Posts:
- [Fatal Error] XML The markup in the document following the root element must be well-formed. - Java
- How to rename a tab in Notepad++ - NotepadPlusPlus
- Show Hide SharePoint column in List Library form with the conditional formula - SharePoint
- Fix Microsoft Teams Admin Center error - The Security zone setting isn't configured correctly - Teams
- Make Notepad++ the default App for .txt file extensions - NotepadPlusPlus
- Chessboard with pieces using pure HTML and CSS - Html
- Android Alert Dialog with Checkboxes example - Android
- [Solved] com.sun.xml.ws.transport.http.servlet.WSServletContextListener ClassNotFoundException - Java