From b7b2a9e50fd48df2fa202f00f8b943cd53fefd08 Mon Sep 17 00:00:00 2001 From: SleekPanther Date: Mon, 24 Apr 2017 07:04:03 -0400 Subject: [PATCH 1/2] Reference static fields from the class not the object instance --- Exercise_09/Exercise_09_05/Exercise_09_05.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Exercise_09/Exercise_09_05/Exercise_09_05.java b/Exercise_09/Exercise_09_05/Exercise_09_05.java index be543632..feecd264 100644 --- a/Exercise_09/Exercise_09_05/Exercise_09_05.java +++ b/Exercise_09/Exercise_09_05/Exercise_09_05.java @@ -21,8 +21,8 @@ public static void main(String[] args) { // Display the current year, month, and day in format Mth/Day/Year System.out.print("\nCurrent year, month, and day in format Mth/Day/Year: "); - System.out.println(calender.get(calender.MONTH) + "/" + - calender.get(calender.DAY_OF_MONTH) + "/" + calender.get(calender.YEAR)); + System.out.println(calender.get(GregorianCalendar.MONTH) + "/" + + calender.get(GregorianCalendar.DAY_OF_MONTH) + "/" + calender.get(GregorianCalendar.YEAR)); // Set elapsed time since January 1, 1970 to 1234567898765L calender.setTimeInMillis(1234567898765L); @@ -30,7 +30,7 @@ public static void main(String[] args) { // Display the year, month and day System.out.print("\nElapsed time since January 1, 1970 set to " + "1234567898765L in format Mth/Day/Year: "); - System.out.println(calender.get(calender.MONTH) + "/" + - calender.get(calender.DAY_OF_MONTH) + "/" + calender.get(calender.YEAR)); + System.out.println(calender.get(GregorianCalendar.MONTH) + "/" + + calender.get(GregorianCalendar.DAY_OF_MONTH) + "/" + calender.get(GregorianCalendar.YEAR)); } } \ No newline at end of file From 7a36f3902eaf3a34152c22230101375bb444d8ff Mon Sep 17 00:00:00 2001 From: SleekPanther Date: Sat, 8 Jul 2017 16:12:38 -0400 Subject: [PATCH 2/2] Fixed Exercise 19_03 (Actually removes duplicates & returns a new list) --- .../Exercise_19_03/Exercise_19_03.java | 37 +++++++++++-------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/Exercise_19/Exercise_19_03/Exercise_19_03.java b/Exercise_19/Exercise_19_03/Exercise_19_03.java index 56570ea0..401d5380 100644 --- a/Exercise_19/Exercise_19_03/Exercise_19_03.java +++ b/Exercise_19/Exercise_19_03/Exercise_19_03.java @@ -1,22 +1,29 @@ -/********************************************************************************* -* (Distinct elements in ArrayList) Write the following method that returns a new * -* ArrayList. The new list contains the non-duplicate elements from the original * -* list. * -* * -* public static ArrayList removeDuplicates(ArrayList list) * -*********************************************************************************/ +/************************************************************************************** +* (Distinct elements in ArrayList) Write the following method that returns a new * +* ArrayList. The new list contains the non-duplicate elements from the original list. * +* * +* public static ArrayList removeDuplicates(ArrayList list) * +**************************************************************************************/ import java.util.ArrayList; public class Exercise_19_03 { /** Removes duplicate elements from an array list */ - public static > - ArrayList removeDuplicates(ArrayList list) { - for (int i = 0; i < list.size() - 1; i++) { - for (int j = i + 1; j < list.size(); j++) { - if (list.get(i).compareTo(list.get(j)) == 0) - list.remove(j); + public static > ArrayList removeDuplicates(ArrayList list) { + ArrayList newList = new ArrayList(); + if(!list.isEmpty()){ + newList.add(list.get(0)); //adds 1st item from reference list + for (int i = 1; i < list.size(); i++) { //loop starts from 1 + boolean shouldAddToNewList = true; + for (int j = 0; j < newList.size(); j++) { + if (list.get(i).compareTo(newList.get(j)) == 0){ + shouldAddToNewList = false; + } + } + if(shouldAddToNewList){ + newList.add(list.get(i)); + } } } - return list; - } + return newList; + } } \ No newline at end of file