aspose file tools*
The moose likes Java in General and the fly likes How to add the objects to the arraylist class if object has many states Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to add the objects to the arraylist class if object has many states" Watch "How to add the objects to the arraylist class if object has many states" New topic
Author

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

upanshu vaid
Ranch Hand

Joined: Sep 07, 2012
Posts: 73

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
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2628
    
    9

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.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5 OCPBCD5
upanshu vaid
Ranch Hand

Joined: Sep 07, 2012
Posts: 73

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

Joined: Sep 07, 2012
Posts: 73

How can I make comparator to sort according to name and according to sex
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2628
    
    9

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.

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to add the objects to the arraylist class if object has many states