This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer (Exam 1Z0-830) Java SE 17 Developer (Exam 1Z0-829) Programmer’s Guide and have Khalid Mughal and Vasily Strelnikov on-line!
See this thread for details.
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

How to add the objects to the arraylist class if object has many states

 
Ranch Hand
Posts: 83
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
In this below code I want to do that once I set the state of the object I want to add that object to the arraylist

kindly help on this
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your line 5 should have read


Then after you have got the inputs after line 19, add the "sort" to the array list ar.

Don't know why you will need an array (line 6) and a list (line 5) given the list is Sort type.
 
upanshu vaid
Ranch Hand
Posts: 83
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply
one more quick ?



comparator class


How can I make comparator to sort according to and according to sex
 
upanshu vaid
Ranch Hand
Posts: 83
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I make comparator to sort according to name and according to sex
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You want to compare name THEN sex or either name or sex?

If either, then you need 2 comparators one for each. If both, then check if names are equals then check sex.

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

upanshu vaid wrote:How can I make comparator to sort according to name and according to sex


You were a whisker away from it with the comparator you just wrote, but then you returned 'name'.

As K.Tsang said, if you want to order by name THEN sex, then the sex comparison only needs to be done if the names are equal.

The problem is that, for any Comparable object, there are TWO ways of determining if they are "equal":
1. equals() returns true.
2. compareTo() returns 0.
And there is nothing that requires that the two methods work the same (although it's usually a good idea if they do).

Since your Comparator is creating an order, it makes sense to me to use compareTo(), since it is the method that provides an order; so putting all that together, you get (from your own method):or something very like it.

See how close you were?

Winston
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic