package test;interface Idemo{
void foo();
}class CdemoA implements Idemo{
public void foo(){}
}class CdemoB implements Idemo{
public void foo(){}
}public class InterfaceArrayCast { public static void main(String[] args){
Object[] castArray= {new CdemoA(), new CdemoB()};

Idemo[] t = (Idemo[])castArray; 
for(int i = 0; i < t.length; i++){
t[i].foo();
}
}
}运行时,抛出以下错误:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ltest.Idemo;
at test.InterfaceArrayCast.main(InterfaceArrayCast.java:20)也就是在以下这行
Idemo[] t = (Idemo[])castArray;

解决方案 »

  1.   

    interface Idemo{
    void foo();
    }class CdemoA implements Idemo{
    public void foo(){}
    }class CdemoB implements Idemo{
    public void foo(){}
    }public class InterfaceArrayCast { public static void main(String[] args){
    Idemo[] castArray= {new CdemoA(), new CdemoB()};

    //Idemo[] t = (Idemo[])castArray; 
    for(int i = 0; i < castArray.length; i++){
    castArray[i].foo();
    }
    }
    }
    这样就可以了 
      

  2.   

    import java.util.Iterator;interface Idemo{
    void foo();
    }class CdemoA implements Idemo{
    public void foo(){}
    }class CdemoB implements Idemo{
    public void foo(){}
    }public class InterfaceArrayCast { public static void main(String[] args){
    Object[] castArray= {new CdemoA(), new CdemoB()};

    Idemo[] t = new Idemo[2] ;
    for(int i = 0; i < castArray.length; i++){

    t[i]=(Idemo)castArray[i];
    t[i].foo();
    }

    }
    }
    数组好像不能直接这样赋值Idemo[] t = (Idemo[])castArray;
    要分别对数组的每个对象进行赋值:t[i]=(Idemo)castArray[i];
      

  3.   

    错误信息里写的很清楚,Object类型的数据无法转换成Idemo类型
      

  4.   

    package test;
    import java.util.ArrayList;interface Idemo{
    void foo();
    }class CdemoA implements Idemo{
    public void foo(){}
    }class CdemoB implements Idemo{
    public void foo(){}
    }public class leilei {    public static void main(String[] args){
            ArrayList<Idemo> castArray=new ArrayList<Idemo>();
            
            castArray.add(new CdemoA());
            castArray.add(new CdemoB());
            
            Idemo[] shuzu=new Idemo[castArray.size()];
            
            for (int i=0;i<castArray.size();i++)
            {
                shuzu[i]=castArray.get(i);
            }
            
            }
    }用泛型怎么直接赋值啊 我只会一个一个赋值
    用Idemo[] shuzu=castArray.toArray();出错了 
    谁回答一下下。。
      

  5.   

    Idemo[] t = (Idemo[])castArray; 
    接口可以有实例吗
      

  6.   

    这句话的意思是声明一个引用t 指向实现了Idemo这个接口的对象的集合  
    应该是这样的 
    我也是新手 呵呵 说错了莫怪。。
      

  7.   

    直接定义Idemo array或者针对每个item转化
    Idemo[] castArray= {new CdemoA(), new CdemoB()};
    或者
    //Idemo[] t = (Idemo[])castArray;
    for(int i = 0; i < t.length; i++){
    ((Idemo)t[i]).foo();
    }
      

  8.   

    归根到底,如果一个数组的元素为类的实例时,是无法将此数组转型成该类的接口数组的,是吗?如果接口有多种定义,那岂不是很麻烦。现在的情况是,产品的平台有机制获取到某一接口的所有实现类,它是以Object[] 形式返回的(如例子里的castArray)。我在A模块里定义了一个接口Idemo,B,C,D等模块会扩展我的接口,然后在A模块内部,我要用用平台的返回结果集,来执行所有的扩展,这时当然我只能转型为Idemo,因为我根本不知道别人的实现类的信息。于是出现了
    Idemo[] t = (Idemo[])castArray; 
    for(int i = 0; i < t.length; i++){
    t[i].foo();
    这样的调用,到底为什么不行,还没有明确的解释。
      

  9.   

    存对象都用ArrayList或者链表吧