不兼容的double数组类型和属性string.split()

时间:2011-04-27 01:32:33

标签: java compiler-errors

public static void main(String[] args)
{
    String input="jack=susan,kathy,bryan;david=stephen,jack;murphy=bruce,simon,mary";
    String[][] family = new String[50][50];

    //assign family and children to data by ;
    StringTokenizer p = new StringTokenizer (input,";");
    int no_of_family = input.replaceAll("[^;]","").length();
    no_of_family++;
    System.out.println("family= "+no_of_family);
    String[] data = new String[no_of_family];
    int i=0;
    while(p.hasMoreTokens())
    {
        data[i] = p.nextToken();
        i++;
    }

    for (int j=0;j<no_of_family;j++)
    {
        family[j][0] = data[j].split("=")[0];
                    //assign child to data by commas
        StringTokenizer v = new StringTokenizer (data[j],",");
        int no_of_child = data[j].replaceAll("[^,]","").length();
        no_of_child++;

        System.out.println("data from input = "+data[j]);
        for (int k=1;k<=no_of_child;k++)
        {

            family[j][k]= data[j].split("=")[1].split(",");
            System.out.println(family[j][k]);
        }

    }

}

我在输入字符串中有一个家庭列表,我分成一个家庭,我想在双数组家庭[i] [j]中进行。

我的目标是:

family[0][0]=1st father's name
family[0][1]=1st child name
family[0][2]=2nd child name and so on...

family[0][0]=jack
family[0][1]=susan
family[0][2]=kathy
family[0][3]=bryan
family[1][0]=david
family[1][1]=stephen
family[1][2]=jack
family[2][0]=murphy
family[2][1]=bruce
family[2][2]=simon
family[2][3]=mary

但我得到错误作为标题:在兼容类型 实测值:java.lang.String中[] 要求:java.lang.String中 family [j] [k] = data [j] .split(“=”)[1] .split(“,”);

我能做什么?我需要帮助

nyone知道如何使用StringTokenizer进行此输入吗?

3 个答案:

答案 0 :(得分:1)

您正在尝试将字符串数组分配给字符串。也许这会让它更清楚?

String[] array = data.split("=")[1].split(",");

现在,如果你想要那个数组的第一个元素,你可以这样做:

family[j][k] = array[0];

答案 1 :(得分:1)

试图理解为什么你不能只使用split进行嵌套操作。

例如,像这样的东西应该可以正常工作

for (int j=0;j<no_of_family;j++)
{
    String[] familySplit = data[j].split("=");

    family[j][0] = familySplit[0];

    String[] childrenSplit = familySplit[1].split(",");
    for (int k=0;k<childrenSplit.length;k++)
    {

        family[j][k+1]= childrenSplit[k];
    }

}

答案 2 :(得分:1)

我总是避免直接使用数组。与动态列表相比,它们难以操纵。我使用父级地图将这个解决方案实施到儿童Map<String, List<String>>列表中(阅读Map<Parent, List<Children>>)。

public static void main(String[] args) {
        String input = "jack=susan,kathy,bryan;david=stephen,jack;murphy=bruce,simon,mary";

        Map<String, List<String>> parents = new Hashtable<String, List<String>>();
        for ( String family : input.split(";")) {
            final String parent = family.split("=")[0];
            final String allChildrens = family.split("=")[1];

            List<String> childrens = new Vector<String>();
            for (String children : allChildrens.split(",")) {
                childrens.add(children);
            }
            parents.put(parent, childrens);
        }

        System.out.println(parents);
    }

输出是这样的:

{jack=[susan, kathy, bryan], murphy=[bruce, simon, mary], david=[stephen, jack]}

使用此方法,您可以使用地图访问父目录:

System.out.println(parents.get("jack"));

并输出:

[susan, kathy, bryan]