Skip to content

Commit 8135dd0

Browse files
committed
feat: Add VariableArgumentsForStrings to demonstrate varargs with formatted string lists
WHAT the code does: Defines a VariableArgumentsForStrings class with: - showList(int start, String... S): prints each string argument as a numbered list, starting at the given index. In main(): - Calls showList(5, "John","Smith","Ajay","Ahmed","Mark","List of name show you variable arguments"). - Prints the provided names and messages numbered sequentially from 5. WHY this matters: Demonstrates varargs with non-primitive types (String). Shows how to combine fixed parameters (start) with varargs (String... S). Illustrates formatting of list-like output, a common console programming task. Highlights flexibility in method design: caller can pass any number of string arguments. HOW it works: main() passes multiple string arguments along with a starting index. showList iterates over the array S: - Prints each string prefixed with an incrementing number starting at `start`. Example output begins with `5. John`, followed by incremented labels. Tips and gotchas: Varargs must be the last parameter in the method signature; here, start comes first. The numbering logic is flexible—uncommenting `i+1` could start numbering from 1 regardless of the start parameter. Be cautious of formatting: concatenating integers and strings directly may lead to subtle bugs; string formatting methods like String.format() improve readability. If no varargs arguments are passed, the loop simply does not execute. Use-cases: Educational demo of combining fixed parameters with varargs in Java. Useful for creating flexible list-printers, menus, or logging utilities. Foundation for building formatted output utilities that adapt to variable input sizes. Short key: class-variableargumentsforstrings varargs string-list-formatting. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 257d477 commit 8135dd0

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
public class VariableArgumentsForStrings {
22
static void showList(int start, String ...S)
33
{
4-
for(int i=0; i<S.length;i++)
5-
{
4+
for(int i=0; i<S.length;i++) {
65
System.out.println(start+ /*i+1+*/ ". "+S[i]);
76
start++;
87
}
98
}
10-
119
public static void main(String... args) {
1210
showList(5,"John","Smith","Ajay","Ahmed","Mark","List of name show you variable arguments");
1311
}
14-
}
12+
}

0 commit comments

Comments
 (0)