Skip to content

Make ListPrinter.java more safe and readable #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Update ListPrinter.java for 2 ways of printing
Added static to the printList function to enable the usage of way 1 (not needing an instance) but still keep the the original way
  • Loading branch information
Naksh-Rathore authored May 7, 2025
commit 3359d06141efbc2968b090ab5fd96d1f65b83cda
25 changes: 18 additions & 7 deletions generics/ListPrinter.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import java.util.List;

public class ListPrinter {
private <T> void printList(List<T> collection){
private static <T> void printList(List<T> collection){ // Remove static if you are doing way 2
for(T item: collection){
System.out.print(item + " ");
}
Expand All @@ -14,15 +14,26 @@ public static void main(String[] args){
List<Double> doubles = List.of(1.1,2.2,3.3,4.4,5.5);
List<Character> chars = List.of('J','O','S','D','E','M');

ListPrinter printer = new ListPrinter();

// Way 1
System.out.print("Integers: ");
printer.printList(integers);
printList(integers);

System.out.print("Doubles: ");
printer.printList(doubles);
printList(doubles);

System.out.print("Characters: ");
printer.printList(chars);
printList(chars);

// Way 2
// ListPrinter printer = new ListPrinter();

// System.out.print("Integers: ");
// printer.printList(integers);

// System.out.print("Doubles: ");
// printer.printList(doubles);

// System.out.print("Characters: ");
// printer.printList(chars);
}
}
}