Chinaunix首页 | 论坛 | 博客
  • 博客访问: 163949
  • 博文数量: 46
  • 博客积分: 2981
  • 博客等级: 少校
  • 技术积分: 475
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-01 12:43
文章分类

全部博文(46)

文章存档

2010年(4)

2009年(9)

2008年(33)

我的朋友

分类: Java

2008-09-21 08:30:36

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>

阅读(614) | 评论(0) | 转发(0) |
0

上一篇:如何提高生活质量

下一篇:节省读书时间

给主人留下些什么吧!~~