从类型对象转换为类

时间:2022-12-08 11:42:55

I'm currently trying to populate an array of of objects of the type Stipulations which is a class which is an

我目前正在尝试填充一组Stipulations类型的对象,这是一个类

public abstract interface

My method of populating this array is as follows where popStipAttr is a simple switch statement.

我填充此数组的方法如下,其中popStipAttr是一个简单的switch语句。

    public static Stipulations[] popStipArr(ZASAllocation zasAlloc) 

    {
          //ArrayList<String> s = new ArrayList<String>();
          ArrayList<Stipulations> stipAL  = new ArrayList<Stipulations>();

          for(int i = 0; i < NoStip; i++)
          {
          stipAL.add(popStipAttr(i,zasAlloc));
          }

          Stipulations[] StipArr =  (Stipulations[]) stipAL.toArray();
          return StipArr;
    }

However I get the error about casting:

但是我得到关于强制转换的错误:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Lc.Stipulations;

What exactly am I doing wrong here I created the arraylist of the same type, why would coverting it to an array of that type throw this?

究竟我在这里做错了什么我创建了相同类型的arraylist,为什么要将它转换为该类型的数组抛出这个?

2 个解决方案

#1


0  

What you are doing in

你在做什么

Stipulations[] StipArr =  (Stipulations[]) stipAL.toArray();

is calling the method on the java.util.List class

正在java.util.List类上调用该方法

Object[] toArray(); 

which is returning you an Object[] which cant be cast to your Stipulations[]
What you want to be calling is the method on java.util.List

这会返回一个Object [],它不能被强制转换为你的规定[]你想要调用的是java.util.List上的方法

<T> T[] toArray(T[] a);

which will return you the array with your type. So try

这将返回您的类型数组。所以试试吧

Stipulations[] StipArr =  stipAL.toArray(new Stipulations[stipAL.size()]);

It is a strange syntax which tends to pollute the code and for this reason and a few others I always try to use Lists where possible and never convert to and from Arrays unless absolutely necessary like an external API not under my control requires it

这是一种奇怪的语法,往往会污染代码,出于这个原因和其他一些我总是尽量使用Lists并且永远不会转换为Arrays和从Arrays转换,除非绝对必要,就像不受我控制的外部API需要它

#2


4  

ArrayList.toArray returns an Object[] (which, as stated in the message, can't be cast to Stipulations[]). ArrayList.toArray(T[] a) however, returns a T[]. Thus, change

ArrayList.toArray返回一个Object [](如消息中所述,不能转换为Stipulations [])。然而,ArrayList.toArray(T [] a)返回T []。因此,改变

Stipulations[] StipArr =  (Stipulations[]) stipAL.toArray();

to

Stipulations[] StipArr =  (Stipulations[]) stipAL.toArray(new Stipulations[0]);
                                                          ^^^^^^^^^^^^^^^^^^^

Oh, right. Just realized what may have caused the confusion. The leading [ in [Ljava.lang.Object; indicates that it is an array. Same for [Lc.Stipulations;. Perhaps that's why you wrote Casting from type object to a class as title :-) Now you know anyway :-)

啊对。刚刚意识到可能导致混乱的原因。领先[在[Ljava.lang.Object;表示它是一个数组。同样的[Lc.Stipulations;。也许这就是为什么你把类型对象的Casting写成标题的类:-)现在你知道了:-)

#1


0  

What you are doing in

你在做什么

Stipulations[] StipArr =  (Stipulations[]) stipAL.toArray();

is calling the method on the java.util.List class

正在java.util.List类上调用该方法

Object[] toArray(); 

which is returning you an Object[] which cant be cast to your Stipulations[]
What you want to be calling is the method on java.util.List

这会返回一个Object [],它不能被强制转换为你的规定[]你想要调用的是java.util.List上的方法

<T> T[] toArray(T[] a);

which will return you the array with your type. So try

这将返回您的类型数组。所以试试吧

Stipulations[] StipArr =  stipAL.toArray(new Stipulations[stipAL.size()]);

It is a strange syntax which tends to pollute the code and for this reason and a few others I always try to use Lists where possible and never convert to and from Arrays unless absolutely necessary like an external API not under my control requires it

这是一种奇怪的语法,往往会污染代码,出于这个原因和其他一些我总是尽量使用Lists并且永远不会转换为Arrays和从Arrays转换,除非绝对必要,就像不受我控制的外部API需要它

#2


4  

ArrayList.toArray returns an Object[] (which, as stated in the message, can't be cast to Stipulations[]). ArrayList.toArray(T[] a) however, returns a T[]. Thus, change

ArrayList.toArray返回一个Object [](如消息中所述,不能转换为Stipulations [])。然而,ArrayList.toArray(T [] a)返回T []。因此,改变

Stipulations[] StipArr =  (Stipulations[]) stipAL.toArray();

to

Stipulations[] StipArr =  (Stipulations[]) stipAL.toArray(new Stipulations[0]);
                                                          ^^^^^^^^^^^^^^^^^^^

Oh, right. Just realized what may have caused the confusion. The leading [ in [Ljava.lang.Object; indicates that it is an array. Same for [Lc.Stipulations;. Perhaps that's why you wrote Casting from type object to a class as title :-) Now you know anyway :-)

啊对。刚刚意识到可能导致混乱的原因。领先[在[Ljava.lang.Object;表示它是一个数组。同样的[Lc.Stipulations;。也许这就是为什么你把类型对象的Casting写成标题的类:-)现在你知道了:-)