Board index » jbuilder » Collections.sort(List)

Collections.sort(List)


2004-12-03 01:31:15 AM
jbuilder15
Hi there,
I'm trying to understand how to fix a warning message from the Java 1.5
compiler that states:
C:\JavaProjects\CardGames\games>javac -Xlint:unchecked *.java
Deck.java:157: warning: [unchecked] unchecked method invocation:
<T>sort(java.ut
il.List<T>) in java.util.Collections is applied to
(java.util.List<games.Card>)
Collections.sort(deck);
I'm trying to use the Collections.sort() method to sort a List object. In
my class I initialize the List object like so:
private List<games.Card>deck = null;
public Deck()
{
Card[] deckOfCards = new Card[52];
//Load up the deck with all necessary cards
for(int i = 0; i < deckOfCards.length; i++)
{
deckOfCards[i] = new Card(vrank[i%13], srank[i/13]);
deckOfCards[i].setAlias(aliases[i]);
}
deck = Arrays.asList(deckOfCards);
}
Then, later I have a sort method like so:
public void SortByValue()
{
Collections.sort(deck); //<<< This generates the warning noted above
}
I don't get it! When I created the List object I specified the type:
<games.Card>
So what's its problem?
Please advise,
Alan
 
 

Re:Collections.sort(List)

Alan Shiers wrote:
Quote
C:\JavaProjects\CardGames\games>javac -Xlint:unchecked *.java
Deck.java:157: warning: [unchecked] unchecked method invocation:
<T>sort(java.ut
il.List<T>) in java.util.Collections is applied to
(java.util.List<games.Card>)
Collections.sort(deck);
How is Card defined? Does it implement Comparable<Card>?
--
Gillmer J. Derge [TeamB]
 

Re:Collections.sort(List)

Quote
How is Card defined? Does it implement Comparable<Card>?

--
Gillmer J. Derge [TeamB]
Yes it does.
*******************************
package games;
import java.util.*;
import javax.swing.*;
/**
* <p>Title: Card</p>
* <p>Description: You can create 52 instances of this class, each
instance<br>
* containing a CardValue and a Suit</p>
* <p>You can use any container to hold the Cards, even a Hashtable if so
desired.
* @author Alan Shiers
* @version 1.0
*/
public class Card extends JLabel implements Comparable
{
private Suit suit;
private CardValue value;
private volatile int hash = 0;
private String alias = "";
public Card(CardValue value, Suit s )
{
suit = s;
this.value = value;
}
/**
* Set an alias name for this Card for identification
* @param st String
*/
public void setAlias(String st)
{
alias = st;
}
/**
* Get this Card(s) alias name
* @return String
*/
public String getAlias()
{
return alias;
}
/**
* Get the value of this Card
* @return CardValue
*/
public CardValue getValue()
{
return value;
}
/**
* Get the suit of this Card
* @return Suit
*/
public Suit getSuit()
{
return suit;
}
/**
* This compareTo method will only return either -1 for less than,
* or 1 for greater than results. No two cards are equal in a
* standard deck of cards. Therefore, this method does not return zero.
* This method will throw a ClassCastException if a Card type object
* is not passed as a parameter.
* @return int Either -1 or 1
* @param o Object expected to be a Card type
*/
public int compareTo(Object o)
{
//To compare cards, we first need to determine the ranking
//of the suits, then their values. The Deck class contains
//a default ranking of the Suits and a mutator method
//to change the default ranking.
Suit[] rank = Deck.getSuitRanking();
//Match the location of the card suit with that
//of the rank array.
int otherCardSuit = 0;
int thisCardSuit = 0;
for(; otherCardSuit < rank.length; otherCardSuit++)
{
if(((Card)o).suit == rank[otherCardSuit])
break;
}
for(; thisCardSuit < rank.length; thisCardSuit++)
{
if(((Card)o).suit == rank[thisCardSuit])
break;
}
//Now, we check the values of each card
List list = CardValue.VALUES;
if(thisCardSuit == otherCardSuit)
{
//In the same suit you can't have two identicle values.
//Therefore, we only need to test if this cards value
//is less than or greater than the other cards value.
if(list.indexOf(this.value) < list.indexOf(((Card)o).value))
return -1;
else
return 1;
}
else if(thisCardSuit < otherCardSuit)
{
return -1;
}
return 1;
}
/**
* Find out if this Card is equal to the one being passed as a parameter.
* @param o Object expected to be of type Card
* @return boolean True if equal, False otherwise
*/
public boolean equals(Object o)
{
if(!(o instanceof Card))
return false;
if(this.suit == ((Card)o).suit)
{
if(this.value == ((Card)o).value)
return true;
else
return false;
}
return false;
}
/**
* For Hashtables, obtain a unique hashcode.
* @return int The hashcode.
*/
public int hashCode()
{
if(hash == 0)
{
int result = 17;
result = 37 * result + calcHash(suit.toString());
result = 37 * result + calcHash(value.toString());
hash = result;
}
return hash;
}
private int calcHash(String st)
{
int i = 0;
int j = 0;
char ac[] = st.toCharArray();
for(int l = 0; l < ac.length; l++)
i = 31 * i + ac[j++];
return i;
}
/**
* Obtain a string representation of this Card.
* @return String
*/
public String toString()
{
return value + " of " + suit;
}
}
 

{smallsort}

Re:Collections.sort(List)

Quote
How is Card defined? Does it implement Comparable<Card>?
Damn! They went and changed Comparable too!! I didn't see that at first.
My class Card did implement Comparable but it was based on the old java 1.4.
I changed it and this seemed to compile fine. Do you see anything else I
should be aware of?
Is the signature for the compareTo() method correct?
Alan
package games;
import java.util.*;
import javax.swing.*;
/**
* <p>Title: Card</p>
* <p>Description: You can create 52 instances of this class, each
instance<br>
* containing a CardValue and a Suit</p>
* <p>You can use any container to hold the Cards, even a Hashtable if so
desired.
* @author Alan Shiers
* @version 1.0
*/
public class Card extends JLabel implements Comparable<Card>
{
private Suit suit;
private CardValue value;
private volatile int hash = 0;
private String alias = "";
public Card(CardValue value, Suit s )
{
suit = s;
this.value = value;
}
/**
* Set an alias name for this Card for identification
* @param st String
*/
public void setAlias(String st)
{
alias = st;
}
/**
* Get this Card(s) alias name
* @return String
*/
public String getAlias()
{
return alias;
}
/**
* Get the value of this Card
* @return CardValue
*/
public CardValue getValue()
{
return value;
}
/**
* Get the suit of this Card
* @return Suit
*/
public Suit getSuit()
{
return suit;
}
/**
* This compareTo method will only return either -1 for less than,
* or 1 for greater than results. No two cards are equal in a
* standard deck of cards. Therefore, this method does not return zero.
* This method will throw a ClassCastException if a Card type object
* is not passed as a parameter.
* @return int Either -1 or 1
* @param o Object expected to be a Card type
*/
public int compareTo(Card o)
{
//To compare cards, we first need to determine the ranking
//of the suits, then their values. The Deck class contains
//a default ranking of the Suits and a mutator method
//to change the default ranking.
Suit[] rank = Deck.getSuitRanking();
//Match the location of the card suit with that
//of the rank array.
int otherCardSuit = 0;
int thisCardSuit = 0;
for(; otherCardSuit < rank.length; otherCardSuit++)
{
if(o.suit == rank[otherCardSuit])
break;
}
for(; thisCardSuit < rank.length; thisCardSuit++)
{
if(o.suit == rank[thisCardSuit])
break;
}
//Now, we check the values of each card
List list = CardValue.VALUES;
if(thisCardSuit == otherCardSuit)
{
//In the same suit you can't have two identicle values.
//Therefore, we only need to test if this cards value
//is less than or greater than the other cards value.
if(list.indexOf(this.value) < list.indexOf(o.value))
return -1;
else
return 1;
}
else if(thisCardSuit < otherCardSuit)
{
return -1;
}
return 1;
}
/**
* Find out if this Card is equal to the one being passed as a parameter.
* @param o Object expected to be of type Card
* @return boolean True if equal, False otherwise
*/
public boolean equals(Object o)
{
if(!(o instanceof Card))
return false;
if(this.suit == ((Card)o).suit)
{
if(this.value == ((Card)o).value)
return true;
else
return false;
}
return false;
}
/**
* For Hashtables, obtain a unique hashcode.
* @return int The hashcode.
*/
public int hashCode()
{
if(hash == 0)
{
int result = 17;
result = 37 * result + calcHash(suit.toString());
result = 37 * result + calcHash(value.toString());
hash = result;
}
return hash;
}
private int calcHash(String st)
{
int i = 0;
int j = 0;
char ac[] = st.toCharArray();
for(int l = 0; l < ac.length; l++)
i = 31 * i + ac[j++];
return i;
}
/**
* Obtain a string representation of this Card.
* @return String
*/
public String toString()
{
return value + " of " + suit;
}
}
 

Re:Collections.sort(List)

Alan Shiers wrote:
Quote
Do you see anything else I should be aware of?
No. I guessed that it was related to Comparable, because the signature
for sort is:
public static <T extends Comparable<? super T>>void sort(List<T>list)
You obviously have a List<T>, so the issue had to be the constraints on
T, but I wasn't able to duplicate the error in my tests. I tried not
implementing Comparable at all and implementing Comparable<Card>but I
didn't try implementing a non-genericized Comparable.
Quote
Is the signature for the compareTo() method correct?
Yes. For Comparable<T>, the signature is int compareTo(T object).
JBuilder will tell you if it's wrong, though in my experiments I
discovered that ErrorInsight doesn't automatically create the correct
signature if you tell it to implement the abstract methods.
--
Gillmer J. Derge [TeamB]