Rayyyyy20569 2021-03-18 18:14 采纳率: 100%
浏览 112
已采纳

关于打印出@加哈希值

打印出的是这些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));
        }
     
        }

    }
  • 写回答

4条回答 默认 最新

  • storm_huang 2021-03-18 18:24
    关注

    test类加一个toString的方法,返回String,具体内容看你自己想要的打印结果。如:

    public String toString(){
        return "myEvent="+myEvent+" , myEventValue="+myEventValue;
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3