如何检查自定义集合中元素的java数据类型?

发布于 2024-10-28 07:03:32 字数 605 浏览 2 评论 0原文

Object ele=a.get(i);
if(ele instanceof java.lang.Integer){//cast to integer:
    print("found Int");
}else{ //cast to string:
    print("found: "+ele.getClass());
}
//prints: found: class com.cycling74.max.Atom$IntAtom

这是更大的代码块的一部分,但这是相关的部分。我需要知道如何通过 骑自行车74

if(ele instanceof com.cycling74.max.Atom$IntAtom)
//ERROR: com.cycling74.max.Atom.IntAtom has private access in com.cycling74.max.Atom

有什么想法吗? 非常感谢 - 这真的让我很头疼!

Object ele=a.get(i);
if(ele instanceof java.lang.Integer){//cast to integer:
    print("found Int");
}else{ //cast to string:
    print("found: "+ele.getClass());
}
//prints: found: class com.cycling74.max.Atom$IntAtom

This is part of a larger chunk of code but this is the relevant part. I need to know how to check the type of an element in the Atom class by cycling74.

if(ele instanceof com.cycling74.max.Atom$IntAtom)
//ERROR: com.cycling74.max.Atom.IntAtom has private access in com.cycling74.max.Atom

Any ideas??
Thanks a lot - this is really doing my head in!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

俏︾媚 2024-11-04 07:03:32

是的,即使不公开 IntAtom 也是可能的,尽管这有点 hacky。

首先,您需要获取对私有内部IntAtom类的引用:

public class SomeClass {
  public static final Class<?> INT_ATOM_CLASS;
  static {
    Class<?> [] innerClasses = Atom.class.getDeclaredClasses();
    Class<?> intAtomClass = null;
    for (Class<?> klass : innerClasses) {
      if (klass.getSimpleName().equals("IntAtom")) {
        intAtomClass = klass;
        break;
      }
    }
    INT_ATOM_CLASS = intAtomClass;
  }
}

然后进行instanceof检查:

if (SomeClass.INT_ATOM_CLASS.isAssignableFrom(ele.getClass())) {
   // do stuff
}

Javadoc for Class.isAssignableFrom(Class c);

Yes, it is possible without making IntAtom public although it is a bit hacky.

First you need to get a reference to the private inner IntAtom class:

public class SomeClass {
  public static final Class<?> INT_ATOM_CLASS;
  static {
    Class<?> [] innerClasses = Atom.class.getDeclaredClasses();
    Class<?> intAtomClass = null;
    for (Class<?> klass : innerClasses) {
      if (klass.getSimpleName().equals("IntAtom")) {
        intAtomClass = klass;
        break;
      }
    }
    INT_ATOM_CLASS = intAtomClass;
  }
}

Then to do the instanceof check:

if (SomeClass.INT_ATOM_CLASS.isAssignableFrom(ele.getClass())) {
   // do stuff
}

Javadoc for Class.isAssignableFrom(Class c);

从此见与不见 2024-11-04 07:03:32

我认为您必须将 IntAtom 公开,或者在 Atom 中提供一个可以验证对象是否是 IntAtom 的公共函数。现在无法比较类定义,因为它是 Atom 私有的。

I think you'll have to either make IntAtom public, or provide a public function in Atom that can verify whether an object is an IntAtom or not. Right now the class definition can't be compared against because it's private to Atom.

今天小雨转甜 2024-11-04 07:03:32

非常感谢大家 - 似乎 Atom 类有一个名为 isInt() 的方法,可以检查内置数据类型。如果你们没有回来,我就不会寻找它,所以我非常感谢您的帮助!

Thanks a lot fellas - it seems the Atom class has a method somewhere called isInt() that can check the built in datatype. I wouldnt have looked for it had you guys not got back so I really appreciate the help!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文