Skip to content

Reference static fields from the class not the object instance #12

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: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions Exercise_09/Exercise_09_05/Exercise_09_05.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ 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);

// 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));
}
}
37 changes: 22 additions & 15 deletions Exercise_19/Exercise_19_03/Exercise_19_03.java
Original file line number Diff line number Diff line change
@@ -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 <E> ArrayList<E> removeDuplicates(ArrayList<E> 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 <E> ArrayList<E> removeDuplicates(ArrayList<E> list) *
**************************************************************************************/
import java.util.ArrayList;

public class Exercise_19_03 {
/** Removes duplicate elements from an array list */
public static <E extends Comparable<E>>
ArrayList<E> removeDuplicates(ArrayList<E> 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 <E extends Comparable<E>> ArrayList<E> removeDuplicates(ArrayList<E> list) {
ArrayList<E> newList = new ArrayList<E>();
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;
}
}