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 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