所需的java.util.ArrayList<弦乐>发现的java.lang.Object:我不明白,这个错误的原因 [英] required java.util.ArrayList<String>,found java.lang.Object : I do not understand the reason for this error

查看:209
本文介绍了所需的java.util.ArrayList<弦乐>发现的java.lang.Object:我不明白,这个错误的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一些片段在JSP页面中:

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){}

%>

在其他srciplet内的code会产生一个错误所需的java.util.ArrayList&LT;串GT;发现的java.lang.Object 。为什么是这样 ?我不明白这样做的原因。

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.

为什么编译器说的SongList是Object类型的?

Why does the compiler say songList to be of type Object ?

推荐答案

您应该明确在开始声明它:

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>();

然后你说的SongList 的ArrayList 对象的 S,不管是什么来的 = 的权利。分配不会改变这一点,因此这样的:

Then you are saying songList is an ArrayList of Objects, regardless of what is to the right of the =. Assignment doesn't change this, so this:

ArrayList songList = new ArrayList<String>();
songList = StoreSongLink.linkList;

不改变的SongList 的类型,它有效地仍然的ArrayList&LT;对象&gt;

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){}

由于的SongList 的数组列表字符串秒。当你拥有了它,Java的提取每对象的SongList ,并试图将其分配到已宣布向左侧什么的,你告诉它是一个的ArrayList&LT;串GT; 。它不能转换对象的ArrayList&LT;弦乐&GT; (特别是因为对象真的只是一个字符串下面),所以它抛出一个错误。

Because songList is array list of Strings. 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.

这篇关于所需的java.util.ArrayList&LT;弦乐&GT;发现的java.lang.Object:我不明白,这个错误的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆