From ce1289ed8543ae3431454c675cedf8d57aac4004 Mon Sep 17 00:00:00 2001 From: Naksh-Rathore Date: Sun, 4 May 2025 18:54:53 -0500 Subject: [PATCH 1/2] Make the types of the Lists safer, add a newline to the end of printLine and space everything out a bit more --- generics/ListPrinter.java | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/generics/ListPrinter.java b/generics/ListPrinter.java index 4994fb4..d03c83a 100644 --- a/generics/ListPrinter.java +++ b/generics/ListPrinter.java @@ -1,22 +1,28 @@ import java.util.List; -import java.util.Arrays; -public class ListPrinter { +public class ListPrinter { private void printList(List collection){ for(T item: collection){ - System.out.printf("%s ", item); + System.out.print(item + " "); } + + System.out.println(); } public static void main(String[] args){ - var integers = List.of(1,2,3,4,5); - var doubles = List.of(1.1,2.2,3.3,4.4,5.5); - var chars = List.of('J','O','S','D','E','M'); - var printer = new ListPrinter(); + List integers = List.of(1,2,3,4,5); + List doubles = List.of(1.1,2.2,3.3,4.4,5.5); + List chars = List.of('J','O','S','D','E','M'); + + ListPrinter printer = new ListPrinter(); + + System.out.print("Integers: "); printer.printList(integers); - System.out.println(); + + System.out.print("Doubles: "); printer.printList(doubles); - System.out.println(); + + System.out.print("Characters: "); printer.printList(chars); } } \ No newline at end of file From 3359d06141efbc2968b090ab5fd96d1f65b83cda Mon Sep 17 00:00:00 2001 From: Naksh Rathore Date: Wed, 7 May 2025 10:24:43 -0500 Subject: [PATCH 2/2] 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 --- generics/ListPrinter.java | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/generics/ListPrinter.java b/generics/ListPrinter.java index d03c83a..d5dfa95 100644 --- a/generics/ListPrinter.java +++ b/generics/ListPrinter.java @@ -1,7 +1,7 @@ import java.util.List; public class ListPrinter { - private void printList(List collection){ + private static void printList(List collection){ // Remove static if you are doing way 2 for(T item: collection){ System.out.print(item + " "); } @@ -14,15 +14,26 @@ public static void main(String[] args){ List doubles = List.of(1.1,2.2,3.3,4.4,5.5); List 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); } -} \ No newline at end of file +}