문제

ArrayList<StringBuilder> al = new ArrayList<>(
                              Arrays.asList(new StringBuilder[]{
                                  new StringBuilder("Oracle"), 
                                  new StringBuilder("Java"), 
                                  new StringBuilder("Sun"), 
                                  new StringBuilder("DataBase")}));

StringBuilder[] al2array = (StringBuilder[]) al.toArray();

If al.toArray() returns an Object[] which i know that its actually a StringBuilder[], then why cannot i cast it?

도움이 되었습니까?

해결책 2

You most certainly can cast a variable typed as Object[] to one typed as StringBuilder[]. The compiler will not complain, and it will execute without error, IF the source reference does indeed reference a StringBuilder[].

public class ArrayCastTest {
    public static void main(String[] argv) {
        Object[] objArray;
        StringBuilder[] sbArray;
        objArray = getArray();
        sbArray = (StringBuilder[]) objArray;
        System.out.println(sbArray.toString());
    }

    public static Object[] getArray() {
        return new StringBuilder[5];
    }
}

This executes without error:

C:\JavaTools>javac ArrayCastTest.java

C:\JavaTools>java ArrayCastTest
[Ljava.lang.StringBuilder;@76f4da6d

C:\JavaTools>

The important thing to understand is that cast is "overloaded" -- it transforms simple values (like int to char) but it does not transform object references -- it only changes the declared type of the reference. The problem is that ArrayList.toArray() returns an Object[]. To get your Object[] into a StringBuilder[] use System.arraycopy.

다른 팁

Java has no way of knowing (at compile time) what objects are stored in an Object[]. Theoretically, it could contain Integers, Strings and GrumpyCats, which would make the cast to StirngBuilder[] wrong - so java simply does not allow it.

You could, however, use the overriden toArray(StringBuilder[]) to do this safely:

StringBuilder[] al2array = al.toArray(new StringBuilder[al.size]);

Try this ie use the the overloaded method toArray(T[] a) since toArray returns an Object[] and you cannot downcast it to StringBuilder[]. The reason is Java does not allow it ie casting it like that ie, an array of Object is created by toArray(), and you can't make Object into DataObject just by casting it.

So you can use the overriden toArray(StringBuilder[]) like this:-

StringBuilder[] al2array =al.toArray(new StringBuilder[al.size()]);

instead of

StringBuilder[] al2array = (StringBuilder[]) al.toArray();

do like this

StringBuilder[] al2array =al.toArray(new StringBuilder[al.size()]);

If al.toArray() returns an Object[] which i know that its actually a StringBuilder[], then why cannot i cast it?

Because it is not actually a StringBuilder[] - it is an Object[] containing (only) StringBuilder instances. Consider this:

Object[] oa = new Object[2];
oa[0] = new StringBuilder();
oa[1] = new StringBuilder();

Would you expect to be able to cast oa to StringBuilder[]? If you did:

StringBuilder[] sba = (StringBuilder []) oa;

What would happen if you then did:

oa[0] = "";   // oa[0] now contains a String; what does sba[0] contain?
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top