ClassCastException:ArrayList不能转换为String [英] ClassCastException: ArrayList cannot be cast to String

查看:196
本文介绍了ClassCastException:ArrayList不能转换为String的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:


java.lang.ClassCastException:java.util.ArrayList无法强制转换为
java .lang.String

java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.lang.String

这是代码:

public static List<String>produit_cart(ArrayList l)
{   
    List<String>re=new ArrayList();
    List<String>l4=new ArrayList();

    re=(List<String>) l.get(0);

    for(int i=1;i<l.size();i++)
    {
        l4=new ArrayList();

        for(int j=0;j<re.size();j++)
        {
            for(int k=0;k<((ArrayList)l.get(i)).size();k++)
            {
                l4.add(re.get(j) +
                        " " +
                        (String)((ArrayList)l.get(i)).get(k));

            }   
        }

        re=new ArrayList();
        for(int m=0;m<l4.size();m++)
        {
            re.add(l4.get(m));
        }   
    }

    return re;
}

问题出在以下几行:

l4.add(re.get(j)+" "+(String)((ArrayList)l.get(i)).get(k));

我试图将其重写为:

l4.add(re.get(j)+" "+((ArrayList)l.get(i)).get(k));

但它不起作用。我该怎么办?

but it didn't work. What should I do ?

推荐答案

你的括号有问题。

您想在上调用 get(i) get(k) ArrayList ,然后将其强制转换为字符串

You want to call get(i) and get(k) on your ArrayList, then cast that to a String.

您的代码转换为 l.get(i)字符串,然后尝试调用 get(k) 就可以了。

Your code casts the result of l.get(i) to a String, then tries to call get(k) on it.

......实际上,我几乎无法确定你的代码是做什么的,因为它在当前状态下难以阅读。

... Actually, I can barely determine what your code does, because it is so difficult to read in its current state.

以下内容应该可以使用并使代码更容易理解:

The following should work and also make your code much easier to follow:

ArrayList tempList = (ArrayList) l.get(i);
String tail = (String) tempList.get(k);
l4.add(re.get(j) + " " + tail;

真正帮助的事情是确保列表 l 是一个字符串列表列表

One thing that would really help would be to ensure that list l is a list of string lists. Then you wouldn't have to cast at all. You could achive that by using the following declaration (if at all possible):

List<ArrayList<String>> listL = new ArrayList<ArrayList<String>>();






以下是一些建议,说明如何重新编写上述内容并使其更容易理解:


Here are some suggestions of how you could re-write the above and make it much easier to follow:

public static List<String> produitCart(List<ArrayList<String>> listOfLists)
{   
    List<String> returnList = listOfLists.get(0);

    for (int i = 1; i < listOfLists.size(); i++) {

        List<String> listFour = new ArrayList<String>();

        for (int j = 0; j < returnList.size(); j++) {
            ArrayList<String> tempList = listOfLists.get(i);
            for (int k = 0; k < tempList.size(); k++) {
                listFour.add(returnList.get(j) + " " + tempList.get(k));
            }   
        }

        returnList = new ArrayList();

        for (int m = 0; m < listFour.size(); m++) {
            returnList.add(listFour.get(m));
        }   
    }

    return returnList;
}

这篇关于ClassCastException:ArrayList不能转换为String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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