Home > Back-end >  Consult, this code where I went wrong!!!!!!!!!!
Consult, this code where I went wrong!!!!!!!!!!

Time:09-26



 

//the List array
String [] strArr={" apple ", "pear", "banana", "peach"};
List List=Arrays. AsList (strArr);

//array List
String [] arr=(String []) list. The toArray ();

for(int i=0; i<4. I++) {
System. The out. Print (strArr [I]);
System.out.println();
}

CodePudding user response:

Error error display is 6 lines of code

CodePudding user response:

Should be a String [] arr=list. ToArray (new String [list. The size ()]);
Arr=new String or a String [] [al. The size ()];
List. The toArray (arr);

CodePudding user response:

Code tried, no problem, screenshot error have a look

CodePudding user response:

reference 3/f a reply from the underlying program apes looked:
code tried, no problem, screenshot error see


Exception in the thread "main" Java. Lang. ClassCastException: class [Ljava. Lang. Object; always be cast to the class [Ljava. Lang. String; ([Ljava. Lang. Object; and [Ljava. Lang. String; all the module in Java. The base of loader 'bootstrap')
The at hjcProject/text. Text4. Main (text4. Java: 19)
An error code

CodePudding user response:

String [] arr=(String []) list. The toArray (); It's the wrong line

CodePudding user response:

Why type conversion is unusual, toArray () in this method is not the list into an array of

CodePudding user response:


My local is completely OK,
Your error because the Object [] cannot be equivalent to the String []

CodePudding user response:

Object [] arr=list. ToArray ();
You can,

CodePudding user response:

refer to 7th floor from the underlying program apes looked response:

My local is completely OK,
Your error because the Object [] cannot be equivalent to the String []

Why can't convert? I look at my code with you

CodePudding user response:

references from the underlying program ape to look up to reply on the eighth floor:
Object [] arr=list. The toArray ();
You can, so


In this way you can and can't convert reason is what?

CodePudding user response:

reference 5 floor VERDOM reply:
String [] arr=(String []) list. The toArray (); It's the wrong line


What is brother? Why can't convert

CodePudding user response:

11 references hjc159357 response:
Quote: refer to fifth floor VERDOM response:

String [] arr=(String []) list. The toArray (); It's the wrong line


What is brother? Why can't convert



ToArray () this method returns the Object type, the String to turn the Object, but strong Object String is not possible,

CodePudding user response:

11 references hjc159357 response:
Quote: refer to fifth floor VERDOM response:

String [] arr=(String []) list. The toArray (); It's the wrong line


What is brother? Why can't convert


You are the object [] to turn the String [], the bosses said very clear
https://blog.csdn.net/hhhebbb/article/details/88813769

CodePudding user response:

ToArray method without parameter is object type, toArray can add parameters, this parameter can be used to specify a return type, you can add a need with a type parameter,

String [] arr=list. ToArray (new String [0]).

CodePudding user response:

refer to 12 floor qq_39936465 reply:
Quote: reference 11 floor hjc159357 response:

Quote: refer to the fifth floor VERDOM reply:

String [] arr=(String []) list. The toArray (); It's the wrong line


What is brother? Why can't convert



ToArray () this method returns the Object type, the String to turn the Object, but strong Object String is not possible,


Understand, but the 7th floor is strong, why he can run

CodePudding user response:

references 9 f hjc159357 response:
Quote: refer to 7th floor from the underlying program apes looked response:


My local is completely OK,
Your error because the Object [] cannot be equivalent to the String []

Why can't convert? I look at my code with your almost

It is possible that the JDK version? I am the JDK 1.8

CodePudding user response:

List List=Arrays. AsList (strArr); Is you it's the wrong line Arrays led package

CodePudding user response:

Look not to understand, look not to understand

CodePudding user response:

I have no problem, is your code intact, I'm jdk1.8

CodePudding user response:

Look not to understand. Look not to understand

CodePudding user response:

Six lines toArray () returns the Object [] type

CodePudding user response:

Look at my ranking,

CodePudding user response:

I also want to learn,

CodePudding user response:

Ha ha, Java will pick orders!nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull