问题
Following are some snippets from a jsp page:
<%! ArrayList songList = new ArrayList<String>(); %>
<%
songList = StoreSongLink.linkList;
// linkList is a static variable in the class StoreSongLink
// There the linkList is defined as public static ArrayList<String> linkList = new ArrayList<String>();
%>
<%} else {
for (ArrayList<String> list : songList){}
%>
The code inside the else srciplet produces an error required java.util.ArrayList<String> found java.lang.Object
. Why is this ? I do not understand the reason for this.
Why does the compiler say songList to be of type Object ?
回答1:
You should declare it explicitly at the start:
ArrayList<String> songList = new ArrayList<String>();
If you declare it like this:
ArrayList songList = new ArrayList<String>();
Then you are saying songList
is an ArrayList
of Object
s, regardless of what is to the right of the =
. Assignment doesn't change this, so this:
ArrayList songList = new ArrayList<String>();
songList = StoreSongLink.linkList;
does not change the type of songList
, it's still effectively ArrayList<Object>
.
If you fix the declaration, then your for
loop should look like:
for (String list : songList){}
Because songList
is array list of String
s. As you have it, Java extracts each Object
from songList
and tries to assign it to what you have declared to the left of :
, which you have told it is an ArrayList<String>
. It cannot convert the Object
to ArrayList<String>
(especially since the Object
is really just a String
underneath), so it throws an error.
回答2:
for (ArrayList<String> list : songList){} // Error is here
And you have define a reference of ArrayList of Object type and declared a String type ArrayList instance which should be ArrayList<String> songList = new ArrayList<String>();
or ArrayList<String> songList = new ArrayList();
(supports only java 7)
So the Corrected code could be like -
<%! List<String> songList = new ArrayList<String>(); %>
...
for (String list : songList){
}
回答3:
Use:
for (String s: songList) {
// etc.
}
Also ensure that StoreSongLink.linkList
is of type ArrayList<String>
.
回答4:
for (ArrayList<String> list : songList){}
must be
for (Object list : songList){}
Because songList declared type is ArrayList(equal ArrayList)
回答5:
While you assign songList a new ArrayList, you're defining the songList as an arrayList (which is arrayList). That would make it okay to iterate over songList as Objects. But you're actually iterating over songList as ArrayList.
So in short, you need to switch songList to be decared to ArrayList and you need to iterate over songList with Strings, not ArrayList.
回答6:
ArrayList
is, as you probably know, a raw type, when not using parametrized ArrayList<E>
declaration. So you effectively turned it to array list of Object
instead of String
, with your initialization:ArrayList songList = new ArrayList<String>();
回答7:
Your iteration is wrong in
for (ArrayList<String> list : songList){}
songList
contains the list of String
elements not the list of ArrayList<String>
so you need to iterate this way for (Object song: songList){}
or
ArrayList<String> songList = new ArrayList<String>();
for (String song: songList){}
来源:https://stackoverflow.com/questions/11987781/required-java-util-arrayliststring-found-java-lang-object-i-do-not-understan