更改多维数组中单个阵列 [英] change multidimensional array to single array

查看:142
本文介绍了更改多维数组中单个阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用我的array.but split()方法,我不能用单array.this是我的code:

i want to use split() method in my array.but, i cant use single array.this is my code:

output_list = [1 2 3 4,5- 6 7 8,9- 8 7 6]

output_list=[1 2 3 4,5 6 7 8,9 8 7 6]

Arraylist<String> output_list = new ArrayList<String>();
    String[][] test = new String[output_list.size()][];
    for(int i=0; i<output_list.size();i++)
    {
       String temp = output_list.get(i);
       test[i] = temp.split(" ");
    }

我想使用唯一的单阵列测试[]。使用.split()后,我们分割数据output_list的该项目将被插入到测试[]。但问题是,当我改变字符串测试[] []测试[];
我有不兼容的类型error.anyone能告诉我为什么它的发生?用java IM

i want to use only single array test[].and the the item in output_list will be insert into test[] after we split the data using .split(). but the problem is when i change String test[][] to test[]; i got incompatible type error.anyone can tell me why its happened?im using java

推荐答案

我建议使用的临时列表,并在年底转换为一个数组。

I recommend using a temporary list and converting to an array at the end.

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

    //add test values
    output_list.add("1 2 3 4");
    output_list.add("5 6 7 8");
    output_list.add("9 8 7 6");

    //temporary list used until converted to an array at the end
    List<String> temp = new ArrayList<String>();

    //iterate over each string in the output_list
    for(String group : output_list){

        //1. split each group into individual strings
        //2. Convert the array returned from split into a list
        //3. Add that list to the temp list
        temp.addAll(Arrays.asList(group.split(" ")));
    }

    //convert the temp list into a string array
    String[] test = temp.toArray(new String[temp.size()]);

这篇关于更改多维数组中单个阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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