how to convert Objext[] into String[] or any other
String[] str[] = (String[])list.toArray(); // won't work, as toArray() returns Object[] String[] str[] = (String[])list.toArray(new String[0]); // works, creates a new String[] String[] str[] = (String[])list.toArray(newString[list.size()]); // works, returns a reference to the array created inline
|
about instanceof
if (objectA instanceof ClassB)
will yield true if objectA can be upcast to objectB.(if objectA is null return false).
An alternative to instanceof is to check the exact class.
Which is better depends on the situation.
// exact match for a class. if (object.getClass() == MyClass.class)
|
It is usually better to use the Class method
<javadoc>public boolean Class.isInstance(Object obj) Determines if the specified Object is assignment-compatible with the object represented by this Class. This method is the dynamic equivalent of the Java language instanceof operator. The method returns true if the specified Object argument is non-null and can be cast to the reference type represented by this Class object without raising a ClassCastException. It returns false otherwise. </javadoc>
|
阅读(659) | 评论(0) | 转发(0) |