The Artima Developer Community
Sponsored Link

Java Answers Forum
java newbie needs guidance from gurus

2 replies on 1 page. Most recent reply: Mar 20, 2002 2:35 PM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Johnny Cash

Posts: 1
Nickname: data118
Registered: Mar, 2002

java newbie needs guidance from gurus Posted: Mar 20, 2002 1:52 PM
Reply to this message Reply
Advertisement
I've spent hours look everywhere on the internet, books, java api docs for help, but can't seem to get good explaination. i really need someone to explain it to me. i need to sort an array of objects. I know i have to implement the comparable interface, but can't understand how it works. for anyone who can give me some help, i will give you my first born.
this is as far as i got... don't know where to go from here.
Thanks in advance

My program consists of two classes. I have an array of objects i want to sort. At runtime it gave me a NullExpection... something.
//first class
.
.
.
Arrays.sort(myArray); //in main

------------------------------------------------------------
// my second class
class myClass implements Comparable
.
.
.
public int compareTo(Object obj)
{
Customer c=(Customer) obj;
if(this.c>c.amount)
return 1;
else if(this.amount<c.amount)
return -1;
else return 0;
}
}


Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: java newbie needs guidance from gurus Posted: Mar 20, 2002 2:27 PM
Reply to this message Reply

CustomerComparator customercomparator = new CustomerComparator(this); //assumes this is a Customer object
Arrays.sort(myArray, customercomparator);

/** Inner class that implements Comparator interface.
*/
class CustomerComparator implements Comparator{

Customer customer;

public CustomerComparator(Customer customer){
this.customer = customer;
}

public int compare(Object obj1,Object obj2){
int result = -1;
Customer c1=(Customer) obj1;
Customer c2=(Customer) obj2;
if(c1.amount < c2.amount) result = -1;
if(c1.amount == c2.amount) result = 0;
if(c1.amount > c2.amount) result = 1;
return result;
}

public boolean equals(Object obj){
Customer c=(Customer) obj;
return (customer.amount == c.amount);
}

}

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: java newbie needs guidance from gurus Posted: Mar 20, 2002 2:35 PM
Reply to this message Reply
Please take care of your first born. My kids are all grown. I don't need any more.

Hope I helped with your problem though.

Flat View: This topic has 2 replies on 1 page
Topic: slow painting while reading and writing doubles for MB Files Previous Topic   Next Topic Topic: Execute the file

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use