Originally posted by yen cheong:
Hi
I am using the sort( List ) method in the Collections class, but am not sure whether this is correct because of the following 2 points:
- I have passed in an ArrayList object to the method, even though it takes a List object. However this compiles OK
- Using this method modifies the order in which the lines in ArrayList are stored, which we are not supposed to change??!!
Originally posted by yen cheong:
- I have passed in an ArrayList object to the method, even though it takes a List object. However this compiles OK
Using the method to sort the names is ok. Just don't store the names in a new ArrayList (or anywhere else).- Using this method modifies the order in which the lines in ArrayList are stored, which we are not supposed to change??!!
JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Originally posted by yen cheong:
I have a public class and another class which implements the Comparator interface. Do I break up the full name within the compare() method in the latter class?
from the API:This compare() method returns an int- I don't understand how this works with the Collections.sort() method?
JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
The second Q that I have is that I have created a class which implements a certain interface, however within this class I have defined only 1 of 2 methods.
JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Give me a suggestion how to reword it, please.Originally posted by jason adam:
Personally, I think the assignment should be re-worded. I too had a problem trying to figure out how to sort the original collection without modifying it.
JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Originally posted by yen cheong:
Jason
I now know why we did not have to define this other method. It is because this method is defined in the superclass Object.
Yen
. Show the names sorted in order of first name and then by last name without modifying the strings or the ArrayList.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |