如何检查自定义集合中元素的java数据类型?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,即使不公开
IntAtom
也是可能的,尽管这有点 hacky。首先,您需要获取对私有内部
IntAtom
类的引用:然后进行instanceof检查:
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:Then to do the instanceof check:
Javadoc for Class.isAssignableFrom(Class c);
我认为您必须将 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.
非常感谢大家 - 似乎 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!