日期:2014-05-20  浏览次数:21238 次

关于Comparable与compareTo
import java.math.BigInteger;
import java.util.Arrays;
public class Lottery
{
public String[] sortByOdds(String[] rules)
{
Ticket[] tickets=new Ticket[rules.length];
for(int i=0;i<rules.length;i++)
tickets[i]=new Ticket(rules[i]);
Arrays.sort(tickets);

String[] names=new String[rules.length];
for(int i=0;i<rules.length;i++)
names[i] = tickets[i].name;
return names;
}
private class Ticket implements Comparable
{
private String name;
private int choices,blanks;
private boolean sort,unique;
private BigInteger probability;
Ticket(String rule)
{
String[] token=rule.split(":");
this.name = token[0];
token = token[1].trim().split(" ");
this.choices = Integer.valueOf(token[0]);
this.blanks = Integer.valueOf(token[1]);
this.sort = token[2].equalsIgnoreCase("T");
this.unique = token[3].equals("T");

if (!sort && !unique)
probability = BigInteger.valueOf((long)Math.pow(choices, blanks));
else if (!sort && unique)
getBinomial(choices,blanks).multiply(getFactorial(blanks));
else if (sort && !unique)
getBinomial(choices+blanks-1,blanks);
else
getBinomial(choices,blanks);
}
public BigInteger getBinomial(int n,int m)
{
return getFactorial(n).divide(getFactorial(m).multiply(getFactorial(n-m)));
}
public BigInteger getFactorial(int n)
{
BigInteger factorial = BigInteger.ONE;
for (int i=1;i<=n;i++)
factorial.multiply(BigInteger.valueOf(n+1));
return factorial;
}
public int compareTo(Object O)
{
Ticket ticket =(Ticket)O;
if (this.probability.equals(ticket.probability))
return this.name.compareTo(ticket.name);
return this.probability.compareTo(ticket.probability);

}

}
}
编译是无错误的,运行时提示compareTo提示错误,求大神帮忙啊!

------解决方案--------------------
把错误贴出来啊。是不是空指针了?
------解决方案--------------------
错误信息是什么,调试一下,应该很简单

祝楼主好运