Commit d22328f
committed
feat(generic-methods): add generic printArray method for arrays of any type
WHAT:
- Implemented `printArray(T[] array)` as a static generic method.
- Demonstrated usage with both `Integer[]` and `String[]`.
WHY:
- To showcase how a single generic method can handle arrays of different types
without duplicating code for each type.
- Emphasizes type safety and reusability in generic programming.
HOW:
- Declared method with type parameter `<T>`.
- Iterated over the array using an enhanced for-loop to print each element.
- Added calls in `main` to test with integer and string arrays.
BENEFITS:
- Eliminates redundancy compared to writing multiple overloaded methods.
- Increases flexibility — works with any object type.
- Provides a clear example of **generic methods in action**.
REAL-WORLD APPLICATIONS:
- Utility methods in frameworks/libraries (e.g., array to string/logging/validation).
- Generic data exporters or serializers that must handle different object arrays uniformly.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 29e28a5 commit d22328f
1 file changed
+70
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
0 commit comments