【问题标题】:Cast a Java Object to Integer将 Java 对象强制转换为整数
【发布时间】:2012-05-31 16:49:37
【问题描述】:

以下代码显示的错误是:无法将对象转换为整数。我不知道我还要添加什么来投射它。

private RowFilter filter(final int itemsPerPage,final int target) {
        return new RowFilter() {
            public boolean include(Entry entry) {

                /* HERE I AM RECEIVING ERROR: Multiple markers at this line
                - Type mismatch: cannot convert from  Object to int
                - Cannot cast from Object to int */
                int ei = (int) entry.getIdentifier();

                return (target * itemsPerPage <= ei && ei < target
                        * itemsPerPage + itemsPerPage);
            }
        };
    }

【问题讨论】:

  • 它将是Integer(对象)而不是int(原始)。为了尽快获得更好的帮助,请发帖SSCCE
  • 可以把Entry的getidentifier方法签名吗?
  • @AndrewThompson 如果它是一个 Integer 对象,这段代码不应该仍然有效吗,因为它会自动拆箱?
  • 太真实了,一切都会正常工作,但没有SSCCE,很难说OP做错了什么:-)

标签: java swing object integer int


【解决方案1】:

你想要的是:

int ei = ((Integer) entry.getIdentifier()).intValue();

【讨论】:

    【解决方案2】:

    我认为您的方法在这里返回Integer 对象,如果是这种情况,您需要这样做:

    int ei =  ((Integer)entry.getIdentifier()).intValue();
    

    【讨论】:

      猜你喜欢
      • 2011-09-26
      • 1970-01-01
      • 2012-05-30
      • 2013-01-22
      • 2014-01-16
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      相关资源
      最近更新 更多