Skip to content

Commit be2e45b

Browse files
authored
Two More Examples for Break and Continue (eugenp#5974)
Issue: BAEL-2432
1 parent 0459f33 commit be2e45b

File tree

1 file changed

+34
-0
lines changed
  • core-java-lang/src/main/java/com/baeldung/controlstructures

1 file changed

+34
-0
lines changed

core-java-lang/src/main/java/com/baeldung/controlstructures/Loops.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,40 @@ public static void printTextNTimesUpTo50(String textToPrint, int times) {
7070
}
7171
}
7272

73+
/**
74+
* Finds the index of {@code name} in a list
75+
* @param name The name to look for
76+
* @param names The list of names
77+
* @return The index where the name was found or -1 otherwise
78+
*/
79+
public static int findFirstInstanceOfName(String name, String[] names) {
80+
int index = 0;
81+
for ( ; index < names.length; index++) {
82+
if (names[index].equals(name)) {
83+
break;
84+
}
85+
}
86+
return index == names.length ? -1 : index;
87+
}
88+
89+
/**
90+
* Takes several names and makes a list, skipping the specified {@code name}.
91+
*
92+
* @param name The name to skip
93+
* @param names The list of names
94+
* @return The list of names as a single string, missing the specified {@code name}.
95+
*/
96+
public static String makeListSkippingName(String name, String[] names) {
97+
String list = "";
98+
for (int i = 0; i < names.length; i++) {
99+
if (names[i].equals(name)) {
100+
continue;
101+
}
102+
list += names[i];
103+
}
104+
return list;
105+
}
106+
73107
/**
74108
* Prints an specified amount of even numbers. Shows usage of both {@code break} and {@code continue} branching statements.
75109
* @param amountToPrint Amount of even numbers to print.

0 commit comments

Comments
 (0)