打印出的是这些test@1b6d3586 test@4554617c test@74a14482 test@1540e19d test@677327b6 test@14ae5a5 test@7f31245a test@6d6f6e28
想打印出插入的值
不知道list怎么遍历并打印 tostring好像不能用
public class test implements Comparable<test> { public String myEvent; public String myEventValue; public test(String myEvent, String myEventValue){ this.myEvent = myEvent; this.myEventValue = myEventValue; } @Override public int compareTo (test o) { if (o.myEvent == "MouseButton") { if (this.myEventValue == o.myEventValue) { return 0; } else if (Integer.valueOf(this.myEventValue) > Integer.valueOf(o.myEventValue)) { return 1; } else { return -1; } } else if (o.myEvent == "Key") { if (this.myEventValue == o.myEventValue) { return 0; } else if (Integer.valueOf(this.myEventValue) > Integer.valueOf(o.myEventValue)) { return 1; } else { return -1; } }else{ return 000000000;} } }
Main类
import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; public class Main { public static void main(String[] args) { test a = new test("MouseButton", "1"); test b = new test("MouseButton", "2"); test c = new test("MouseButton", "3"); test d = new test("Key", "a"); test e = new test("Key", "s"); test f = new test("Key", "d"); test g = new test("Key", "f"); test h = new test("MouseButton", "1"); ArrayList<test> list = new ArrayList<test>(); list.add(a); list.add(b); list.add(c); list.add(d); list.add(e); list.add(f); list.add(g); list.add(h); System.out.println("普通for循环遍历"); for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } }