File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
core-java-lang/src/main/java/com/baeldung/controlstructures Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff 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.
You can’t perform that action at this time.
0 commit comments