Java Programming Create an array of Strings that contains th
Java Programming:
Create an array of Strings that contains the following:
String[] names = { \"Bill\", \"Mike\", \"Joe\", \"Dave\", \"Sam\", \"Mary\", \"George\", \"Lucy\" }
In your main program, search the array and display all of the elements that begin with \'M\'.
Solution
NamesSearch.java
public class NamesSearch {
public static void main(String[] args) {
String[] names = { \"Bill\", \"Mike\", \"Joe\", \"Dave\", \"Sam\", \"Mary\", \"George\", \"Lucy\" };
System.out.println(\"Display all of the elements that begin with \'M\': \");
for(int i=0; i<names.length; i++){
if(names[i].startsWith(\"M\")){
System.out.println(names[i]);
}
}
}
}
Output:
Display all of the elements that begin with \'M\':
Mike
Mary
![Java Programming: Create an array of Strings that contains the following: String[] names = { \ Java Programming: Create an array of Strings that contains the following: String[] names = { \](/WebImages/6/java-programming-create-an-array-of-strings-that-contains-th-986383-1761506784-0.webp)