• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sorting Names in SortNames

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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??!!
Please can you help as I am very confused!!
Please help
Yen
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Look at the API for ArrayList, and notice the interfaces that it implements. That should tell you why this sort of thing is possible


- Using this method modifies the order in which the lines in ArrayList are stored, which we are not supposed to change??!!


Never quite understood that one myself, but you are on the right track with the method you are using.
Jason
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


No problem since ArrayList implements List, it can be used whereever a List is needed.

- Using this method modifies the order in which the lines in ArrayList are stored, which we are not supposed to change??!!

Using the method to sort the names is ok. Just don't store the names in a new ArrayList (or anywhere else).
 
yen cheong
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Thanks for your replies. When I used the sort method, the names were automatically stored in the ArrayList that I passed in as the parameter for the method.
I am now attempting to sort by last name, but am having problems as I don't understand how to use and what to put in the Comparator class. Do I separate the last name from the full name within the compare method in the class? How do I then use this class in the Collections.sort(param1, param2) method?
Very
Yen
 
yen cheong
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
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?
This compare() method returns an int- I don't understand how this works with the Collections.sort() method?

Yen
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?


That's one way to do it.

This compare() method returns an int- I don't understand how this works with the Collections.sort() method?

from the API:
Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

You don't need to worry about the integer. The sort() method takes care of everything. Just sort using your comparator and print.
 
yen cheong
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marilyn
Please can you explain something that doesn't make sense to me ........ the assignment says not to change ArrayList. However both the sort() methods in the Collections class rearrange my original Arraylist as this is the parameter that I pass.
Can you help
Yen
 
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
see Marilyn's reply to the same question by myself awhile ago
Hope it helps
 
yen cheong
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marilyn, Johannes
Sorry, I think I must be missing something here???!! My code is as below:
Sorry I've removed the code.
As soon as sort() method has been run the names are already re-arranged and stored in nameList, which is shown when I print them out. So I don't understand how we can prevent storing.
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. I then seem able to past a object of this class into the Collections.sort() method, which I shouldn't be able to do as I have not defined all the methods in the class which implements the interface??!!
Thanks
Yen
[This message has been edited by Johannes de Jong (edited September 21, 2001).]
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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. However, since you've been able to print out the arranged list, you are at least doing the first step of the program (as all the other posts have stated, keep going with what you have).

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.


I only had one method defined in my class that implements the interface I believe you are talking about. Looking at the API, it is strange that I didn't get an error since I didn't implement the other method, but it's been a long day and I ain't thinking right.
Anyway, the problem I see you having is that you need to figure out what kind of code you need to put within the certain method of your class that implements the interface you need. If you get the list printing out by the first requirement, move on to the next, and don't ponder about how you did it. Nitpicks will guide you from there.
Hope that helps a little bit,
Jason
 
yen cheong
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The other method is also defined in the String class which are the elements in the list that you are comparing.

The idea is that you are not supposed to create a new ArrayList with last names first.
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

Give me a suggestion how to reword it, please.
 
Johannes de Jong
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey you said it
The idea is that you dont create a new ArrayList with last names first
and I would add something like this as well though :
and use the sort() method to sort that new arraylist
[This message has been edited by Johannes de Jong (edited September 21, 2001).]
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Ah, now it's coming back to me, thanks for the reminder
In regards to the wording, the assignment states:

. Show the names sorted in order of first name and then by last name without modifying the strings or the ArrayList.


That's the part that is confusing. When the sort method rearranges the names, in my mind that's a modification to the ArrayList. Would it be possible just to leave out the ArrayList part, or in your experience would that cause more problems? Either that or put in something about reordering the List is fine, but adding, removing, etc. is not. If not that many people question it, or have a problem with it, then I guess it could be left alone and we can just deal with it as isolated issues pop up
Jason
 
village idiot
Posts: 1208
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought the assignment was quite clear on this. When I first read it, I was thinking that I would just create a second list with the names in a different order until I got to the part that stated that the original list couldn't be modified. Sometimes I get totally confused by the way a problem is phrased, but this wasn't one of those times!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic