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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:10:41  来源:igfitidea点击:

required java.util.ArrayList<String>,found java.lang.Object : I do not understand the reason for this error

javaforeacharraylist

提问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 songListis an ArrayListof Objects, regardless of what is to the right of the =. Assignment doesn't change this, so this:

然后你说songListArrayListObjectS,不管是什么来的右侧=。分配不会改变这一点,所以这个:

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 forloop should look like:

如果您修复了声明,那么您的for循环应如下所示:

for (String list : songList){}

Because songListis array list of Strings. As you have it, Java extracts each Objectfrom songListand 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 Objectto ArrayList<String>(especially since the Objectis really just a Stringunderneath), so it throws an error.

因为songListStrings 的数组列表。正如您所拥有的那样,JavaObject从中提取每个songList并尝试将其分配给您在 左侧声明的内容:,您已经告诉它是一个ArrayList<String>. 它不能转换ObjectArrayList<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){}

songListcontains the list of Stringelements 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

ArrayListis, as you probably know, a raw type, when not using parametrized ArrayList<E>declaration. So you effectively turned it to array list of Objectinstead 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.linkListis 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)