[in java.lang.string; cannot be cast to java.lang.string

I'm getting vector from the product's API

Vector<?> dataVector = dataAPI.getReturnVector();

The expected vector contains a string as a value I can print the size of the vector as 2 But for some reason, I can't iterate and print values

I tried

Iterator<?> iter = dataVector.iterator();

while( iter.hasNext()) {
    System.out.println(iter.next());
}

I always get one

[java.lang.String; cannot be cast to java.lang.String

I used it

iter.next().getClass().getName()

It turned out to be just Java lang.String.

I googled and found http://prideafrica.blogspot.com/2007/01/javalangclasscastexception.html Similar problems were found

I tried to set the generic to string [], but eventually the same error occurred

If the vector contains Java Lang. string, why do I get this cast exception? How to print actual values?

Please provide your suggestions

Solution

Therefore, the API does not return a string vector, but a vector of string []

You should be able to iterate over the vector and then loop through the array for each element

Iterator<String[]> iter = dataVector.iterator();

while( iter.hasNext()) {
    String[] array = iter.next();
    for(int i=0; i < array.length; i++)
    {
       System.out.println(i + ": " + array[i]);
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>