我想通过泛型来对任意类(包含compareto方法)对象进行排序,写了一个例子如下。但是运行不出结果!还有如果我要传进的是Ingeger类型数组a,要对a[0],a[1]比较大小应该怎么写啊?是不是用Integer类中的compareTo方法?如何写?谢谢
//t类
public class t {
private int x;
public t(int c)
{
x=c;
}
public int compareTo(t a,t b)
{
if(a.x>b.x)
return 1;
else return 2;
}
}
//泛型
public class fanxing {
public static <T> Object x(T[] a) {
Object re = 3;
try {
Class c = a.getClass();
Object pp = c.newInstance();
Method m = c.getMethod("compareTo", new Class[] { c, c });
re = m.invoke(pp, new Object[] { a[0], a[1] });
return re;
} catch (Exception e) {
System.out.print("Error");
} finally {
return re;
}
} public static void main(String args[]) {
t[] x = new t[2];
x[1] = new t(2);
x[0] = new t(3); System.out.print(fanxing.<t> x(x)); }
}
//t类
public class t {
private int x;
public t(int c)
{
x=c;
}
public int compareTo(t a,t b)
{
if(a.x>b.x)
return 1;
else return 2;
}
}
//泛型
public class fanxing {
public static <T> Object x(T[] a) {
Object re = 3;
try {
Class c = a.getClass();
Object pp = c.newInstance();
Method m = c.getMethod("compareTo", new Class[] { c, c });
re = m.invoke(pp, new Object[] { a[0], a[1] });
return re;
} catch (Exception e) {
System.out.print("Error");
} finally {
return re;
}
} public static void main(String args[]) {
t[] x = new t[2];
x[1] = new t(2);
x[0] = new t(3); System.out.print(fanxing.<t> x(x)); }
}
首页| 空间| 新闻| 论坛| 博客| 下载| 读书| 网摘| 视频| 书店| 程序员| 求职招聘| 项目交易| 培训| 网址 欢迎您:a276202460|退出|登录注册|帮助
CSDN-CSDN社区-Java-Java SE
管理菜单 生成帖子 置顶 推荐 取消推荐 锁定 解锁 移动 编辑 删除 帖子加分 结 帖 发 帖 回 复 收藏 不显示删除回复显示所有回复显示星级回复显示得分回复 请教高手关于反射和泛型的问题!!谢谢[问题点数:30分]
happyperson
(happyperson) 等 级:
结帖率:90.91%
楼主发表于:2009-10-21 19:56:53我想通过泛型来对任意类(包含compareto方法)对象进行排序,写了一个例子如下。但是运行不出结果!还有如果我要传进的是Ingeger类型数组a,要对a[0],a[1]比较大小应该怎么写啊?是不是用Integer类中的compareTo方法?如何写?谢谢
//t类
public class t {
private int x;
public t(){
}
public t(int c)
{
x=c;
}
public int compareTo(t a,t b)
{
if(a.x>b.x)
return 1;
else return 2;
}
}
//泛型
public class fanxing {
public static <T> Object x(T[] a) {
Object re = 3;
try {
Class c = a[0].getClass();
Object pp = c.newInstance();
Method m = c.getMethod("compareTo", new Class[] { c, c });
re = m.invoke(pp, new Object[] { a[0], a[1] });
return re;
} catch (Exception e) {
System.out.print("Error");
} finally {
return re;
}
} public static void main(String args[]) {
t[] x = new t[2];
x[1] = new t(2);
x[0] = new t(3); System.out.print(fanxing. <t> x(x)); }
}
看红色的代码 不晓得你要干嘛 就是为了排序吗 排序的话实现compareable接口就行了
import java.util.Date;
import java.util.Vector;/**
* @author a276202460
*
*/
public class Compartor extends Vector implements Comparator {
public int colindex; public String st; int i = -1;
public Compartor(int index, String st) {
this.colindex = index;
this.st = st;
if (st.equals("asc")) {
i = 1;
}
} public int compare(Object a, Object b) {
int rs = 0;
Vector v1 = (Vector) a;
Vector v2 = (Vector) b;
Object m = v1.elementAt(colindex);
Object n = v2.elementAt(colindex);
if (m == null && n == null) {
rs = 0 * i;
} else if (m == null) {
rs = -1 * i;
} else if (n == null) {
rs = 1 * i;
}else{
int s;
if (m instanceof String) {
s = m.toString().compareTo(n.toString());
if(s < 0){
rs = -1 * i;
}else if(s > 0 ){
rs = 1 * i;
}else {
rs = 0;
}
}else if(m instanceof Number){
double d1 = Double.parseDouble(m.toString());
double d2 = Double.parseDouble(n.toString());
if(d1 < d2 ){
rs = -1 * i;
}else if(d1 > d2 ){
rs = 1 * i;
}else {
rs = 0;
}
}else if (m instanceof Date){
Date date1 = (Date)m;
Date date2 = (Date)n;
long t1 = date1.getTime();
long t2 = date2.getTime();
if(t1 < t2){
rs = -1 * i;
}else if (t1 > t2){
rs = 1 * i;
}else {
rs = 0;
}
}else if(m instanceof Boolean){
boolean b1 = ((Boolean)m).booleanValue();
boolean b2 = ((Boolean)n).booleanValue();
if(b1 == b2){
rs = 0;
}else if( b1 ){
rs = 1 * i;
}else {
rs = -1 * i;
}
}else {
s = m.toString().compareTo(n.toString());
if(s < 0){
rs = -1 * i;
}else if(s > 0 ){
rs = 1 * i;
}else {
rs = 0;
}
}
}
return rs;
}
}这个事我常用的 给JTABLE排序的类 你参考下