required java.util.ArrayList<String>,found java.lang.Object : 我不明白这个错误的原因
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11987781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
required java.util.ArrayList<String>,found java.lang.Object : I do not understand the reason for this error
提问by Suhail Gupta
Following are some snippets from a jsp page:
以下是jsp页面的一些片段:
<%! 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.
else srciplet 中的代码产生错误required java.util.ArrayList<String> found java.lang.Object
。为什么是这样 ?我不明白这是什么原因。
Why does the compiler say songList to be of type Object ?
为什么编译器说 songList 是 Object 类型?
回答by davidfmatheson
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:
然后你说songList
是ArrayList
的Object
S,不管是什么来的右侧=
。分配不会改变这一点,所以这个:
ArrayList songList = new ArrayList<String>();
songList = StoreSongLink.linkList;
does not change the type of songList
, it's still effectively ArrayList<Object>
.
不会改变 的类型songList
,它仍然有效ArrayList<Object>
。
If you fix the declaration, then your for
loop should look like:
如果您修复了声明,那么您的for
循环应如下所示:
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.
因为songList
是String
s 的数组列表。正如您所拥有的那样,JavaObject
从中提取每个songList
并尝试将其分配给您在 左侧声明的内容:
,您已经告诉它是一个ArrayList<String>
. 它不能转换Object
为ArrayList<String>
(特别是因为Object
实际上只是一个String
底层),所以它会抛出一个错误。
回答by Subhrajyoti Majumder
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)
并且您已经定义了 Object 类型的 ArrayList 的引用并声明了一个 String 类型的 ArrayList 实例,该实例应该是ArrayList<String> songList = new ArrayList<String>();
或ArrayList<String> songList = new ArrayList();
(仅支持 java 7)
So the Corrected code could be like -
所以更正后的代码可能是这样的 -
<%! List<String> songList = new ArrayList<String>(); %>
...
for (String list : songList){
}
回答by subodh
Your iteration is wrong in
你的迭代是错误的
for (ArrayList<String> list : songList){}
songList
contains the list of String
elements not the list of ArrayList<String>
songList
包含String
元素列表而不是元素列表ArrayList<String>
so you need to iterate this way for (Object song: songList){}
所以你需要以这种方式迭代 for (Object song: songList){}
or
或者
ArrayList<String> songList = new ArrayList<String>();
for (String song: songList){}
回答by ohshazbot
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.
当您为 songList 分配一个新的 ArrayList 时,您将 SongList 定义为一个 arrayList(即 arrayList)。这样就可以将 SongList 作为对象进行迭代。但是您实际上是将songList 作为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.
所以简而言之,您需要将 SongList 切换为 ArrayList,并且您需要使用 Strings 而不是 ArrayList 遍历 SongList。
回答by Less
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>();
ArrayList
正如您可能知道的那样,当不使用参数化ArrayList<E>
声明时,它是原始类型。因此,您通过初始化有效地将其转换为数组列表Object
而不是String
:ArrayList songList = new ArrayList<String>();
回答by Reimeus
Use:
利用:
for (String s: songList) {
// etc.
}
Also ensure that StoreSongLink.linkList
is of type ArrayList<String>
.
还要确保StoreSongLink.linkList
类型为ArrayList<String>
。
回答by ice
for (ArrayList<String> list : songList){}
must be
必须是
for (Object list : songList){}
Because songList declared type is ArrayList(equal ArrayList)
因为songList声明的类型是ArrayList(等于ArrayList)