Commit 8135dd0
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
1 file changed
+2
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | | - | |
| 4 | + | |
6 | 5 | | |
7 | 6 | | |
8 | 7 | | |
9 | 8 | | |
10 | | - | |
11 | 9 | | |
12 | 10 | | |
13 | 11 | | |
14 | | - | |
| 12 | + | |
0 commit comments