Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > sorting an ArrayList by int

Reply
Thread Tools

sorting an ArrayList by int

 
 
zcraven
Guest
Posts: n/a
 
      10-25-2004
I need to sort a football league by the number of points each club has. The
clubs are objects stored in an ArrayList called 'league'. Each club has a
field for the number of points they have.

Can someone suggest the best way to sort this arraylist and output it to
screen?

Most examples on the web suggest I type:

collections.sort(league)

but i get the error that it expects an Array but has found an ArrayList.

PLEASE HELP!


 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      10-25-2004
zcraven <(E-Mail Removed)> scribbled the following
on comp.lang.java.programmer:
> I need to sort a football league by the number of points each club has. The
> clubs are objects stored in an ArrayList called 'league'. Each club has a
> field for the number of points they have.


> Can someone suggest the best way to sort this arraylist and output it to
> screen?


> Most examples on the web suggest I type:


> collections.sort(league)


> but i get the error that it expects an Array but has found an ArrayList.


> PLEASE HELP!


There is a Collections.sort(List) method. Simply feed your ArrayList to
it with Collections.sort(league). You have to make sure your club
objects implement the Comparable interface though - otherwise you may
end up with ClassCastExceptions, or a list that is not properly sorted.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"'I' is the most beautiful word in the world."
- John Nordberg
 
Reply With Quote
 
 
 
 
zcraven
Guest
Posts: n/a
 
      10-25-2004
when I type this line in class 'Club' I get the error 'club is not abstract
and does not override abstract method compareTo':

public class Club implements Comparable

I have been stuck on this for 4 hours!

Zac



"Joona I Palaste" <(E-Mail Removed)> wrote in message
news:cljeut$5m9$(E-Mail Removed)...
> zcraven <(E-Mail Removed)> scribbled the following
> on comp.lang.java.programmer:
> > I need to sort a football league by the number of points each club has.

The
> > clubs are objects stored in an ArrayList called 'league'. Each club has

a
> > field for the number of points they have.

>
> > Can someone suggest the best way to sort this arraylist and output it to
> > screen?

>
> > Most examples on the web suggest I type:

>
> > collections.sort(league)

>
> > but i get the error that it expects an Array but has found an ArrayList.

>
> > PLEASE HELP!

>
> There is a Collections.sort(List) method. Simply feed your ArrayList to
> it with Collections.sort(league). You have to make sure your club
> objects implement the Comparable interface though - otherwise you may
> end up with ClassCastExceptions, or a list that is not properly sorted.
>
> --
> /-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
> \-------------------------------------------------------- rules! --------/
> "'I' is the most beautiful word in the world."
> - John Nordberg



 
Reply With Quote
 
Carl Howells
Guest
Posts: n/a
 
      10-25-2004
zcraven wrote:
> when I type this line in class 'Club' I get the error 'club is not abstract
> and does not override abstract method compareTo':
>
> public class Club implements Comparable
>
> I have been stuck on this for 4 hours!
>
> Zac
>


Please don't top-post.

You're getting that error because your class (class names should start
with capital letters) is not declared as abstract, and doesn't override
the abstract method compareTo. The compiler is telling you quite
clearly what's going on.

In order to implement an interface in a non-abstract class, you have to
provide implementations for the methods declared in the interface.
Comparable happens to declare one method, named compareTo. Check the
documentation for Comparable to see what the method's exact signature
should be, and what it should do when you implement it.
 
Reply With Quote
 
zcraven
Guest
Posts: n/a
 
      10-25-2004
This is the whole code of the method I am writing:

public void printLeagueTable()
{
System.out.println("LEAGUE: " + leagueName);
Collections.sort(league);

for (int i=0; i<league.size(); i++)
{
Club club = league[i]; // ERROR 'array required but found
arraylist'
String clubName = club.getClubName();
int points = club.getPointsTally();
System.out.println(clubName + " = " + points);
}
}

(the clubs in the league have other fields but I want to sort them by the
goalTally field)







"zcraven" <(E-Mail Removed)> wrote in message
news:cljffa$jog$(E-Mail Removed)...
> when I type this line in class 'Club' I get the error 'club is not

abstract
> and does not override abstract method compareTo':
>
> public class Club implements Comparable
>
> I have been stuck on this for 4 hours!
>
> Zac
>
>
>
> "Joona I Palaste" <(E-Mail Removed)> wrote in message
> news:cljeut$5m9$(E-Mail Removed)...
> > zcraven <(E-Mail Removed)> scribbled the following
> > on comp.lang.java.programmer:
> > > I need to sort a football league by the number of points each club

has.
> The
> > > clubs are objects stored in an ArrayList called 'league'. Each club

has
> a
> > > field for the number of points they have.

> >
> > > Can someone suggest the best way to sort this arraylist and output it

to
> > > screen?

> >
> > > Most examples on the web suggest I type:

> >
> > > collections.sort(league)

> >
> > > but i get the error that it expects an Array but has found an

ArrayList.
> >
> > > PLEASE HELP!

> >
> > There is a Collections.sort(List) method. Simply feed your ArrayList to
> > it with Collections.sort(league). You have to make sure your club
> > objects implement the Comparable interface though - otherwise you may
> > end up with ClassCastExceptions, or a list that is not properly sorted.
> >
> > --
> > /-- Joona Palaste ((E-Mail Removed)) -------------

Finland --------\
> > \--------------------------------------------------------

rules! --------/
> > "'I' is the most beautiful word in the world."
> > - John Nordberg

>
>



 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      10-25-2004
zcraven wrote:
> when I type this line in class 'Club' I get the error 'club is not abstract
> and does not override abstract method compareTo':
>
> public class Club implements Comparable
>
> I have been stuck on this for 4 hours!


Did you implement a compareTo() method? When
you said that your class "implements Comparable,"
you promised to provide compareTo().

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Andy Hill
Guest
Posts: n/a
 
      10-25-2004
"zcraven" <(E-Mail Removed)> wrote:
>when I type this line in class 'Club' I get the error 'club is not abstract
>and does not override abstract method compareTo':
>
> public class Club implements Comparable
>
>I have been stuck on this for 4 hours!
>
>Zac
>

If you implement the Comparable interface in Club, you gotta have a "public int
compareTo(Object o)" method in Club. Lots of examples on the 'net on how best
to implement compareTo().

 
Reply With Quote
 
zcraven
Guest
Posts: n/a
 
      10-25-2004

"Carl Howells" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> zcraven wrote:
> > when I type this line in class 'Club' I get the error 'club is not

abstract
> > and does not override abstract method compareTo':
> >
> > public class Club implements Comparable
> >
> > I have been stuck on this for 4 hours!
> >
> > Zac
> >

>
> Please don't top-post.
>
> You're getting that error because your class (class names should start
> with capital letters) is not declared as abstract, and doesn't override
> the abstract method compareTo. The compiler is telling you quite
> clearly what's going on.
>
> In order to implement an interface in a non-abstract class, you have to
> provide implementations for the methods declared in the interface.
> Comparable happens to declare one method, named compareTo. Check the
> documentation for Comparable to see what the method's exact signature
> should be, and what it should do when you implement it.


I am just a beginner to java and I dont really follow what you mean in your
2nd paragraph. Basically I want to iterate all clubs in the league, getting
the number of points from each one and putting them into order. When I try
to get a club from the arraylist, it says it requires an array when I am
using an arraylist. Thanks.


 
Reply With Quote
 
zcraven
Guest
Posts: n/a
 
      10-25-2004

"Eric Sosman" <(E-Mail Removed)> wrote in message
news:cljggs$8$(E-Mail Removed)...
> zcraven wrote:
> > when I type this line in class 'Club' I get the error 'club is not

abstract
> > and does not override abstract method compareTo':
> >
> > public class Club implements Comparable
> >
> > I have been stuck on this for 4 hours!

>
> Did you implement a compareTo() method? When
> you said that your class "implements Comparable,"
> you promised to provide compareTo().
>
> --
> (E-Mail Removed)
>


The Club class should not abstract so it seems I cannot include the line
'implements Comparable'


 
Reply With Quote
 
zcraven
Guest
Posts: n/a
 
      10-25-2004

"zcraven" <(E-Mail Removed)> wrote in message
news:cljh01$c3f$(E-Mail Removed)...
>
> "Eric Sosman" <(E-Mail Removed)> wrote in message
> news:cljggs$8$(E-Mail Removed)...
> > zcraven wrote:
> > > when I type this line in class 'Club' I get the error 'club is not

> abstract
> > > and does not override abstract method compareTo':
> > >
> > > public class Club implements Comparable
> > >
> > > I have been stuck on this for 4 hours!

> >
> > Did you implement a compareTo() method? When
> > you said that your class "implements Comparable,"
> > you promised to provide compareTo().
> >
> > --
> > (E-Mail Removed)
> >

>
> The Club class should not abstract so it seems I cannot include the line
> 'implements Comparable'
>
>


How about I forget ArrayList and use a different method to display the
league instead? I just need to access all the clubs, return the number of
points of each, sort these results, and then display them to the user.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
int a[10]; int* p=(int*)((&a)+1); But why p isn't equal to ((&a)+1)? aling C++ 8 10-20-2005 02:42 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
Really simple question: How to convert ArrayList of int to int[]? Homa ASP .Net 4 11-14-2003 01:18 AM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments