如何拆分ArrayList并将数据存储到android中的String数组中?

时间:2012-03-19 13:50:25

标签: java android arrays string arraylist

在我的应用程序中,我使用“|”将字符串类型数据存储在arraylist中operator作为分隔符。即我的数组列表包含数据        [| 123124,|,324543 | 789649666356] 现在我需要用“|”拆分arraylist并需要将数据存储到字符串数组中。我怎么能这样做?

我的代码:

ArrayList<String> arr2;
ArrayList<ArrayList<String>> alllist;
String[] str;

for (j = 0; j < alllist.size(); j++) {
    arr2 = new ArrayList<String>();
    arr2 = alllist.get(j);

    for (Object v : arr2) {
        str = v.toString().split("|");
        System.out.println("checking str[i]" + str[i]);

        if (str[j].contains(",")) {
            System.out.println("hii");
        } 
    }
}

if(str[j].contains(","))行显示错误....请帮我解决这个问题.. 提前谢谢......

3 个答案:

答案 0 :(得分:1)

不确定要实现的目标,但这可以帮助您构建代码:

public static void main(String args[]) throws ParseException {

    List<List<String>> allList = new ArrayList<List<String>>();
    allList.add(Arrays.asList("|123,124,|,324,543|789,649,666,356"));

    for (List<String> list : allList) { //use a for each loop instead of looping on an int and using allList.get(i)
        for (String s : list) { //Same thing
            String[] str = s.split("\\|"); //You need to escape | with \\ to get the expected split
            for (String item : str) {
                System.out.println("checking " + item);
                if (item.contains(",")) {
                    System.out.println("hii");
                }
            }

        }
    }
}

答案 1 :(得分:1)

为什么不简单地使用它:

List<String> flag2=new ArrayList<String>();
String flag[]=new String[flag2.size()];

for(int i=0;i<flag2.size();i++){
    flag[i]=flag2.get(i);
}

答案 2 :(得分:0)

public void onClick(View v){
    String teks = txtinput.getText().toString();
    String[] tempPesanMasuk = teks.split("_");
    ArrayList<String> teks_lengkap = new ArrayList<String>();
    String teksJadi = "";

    for(int i = 0; i < tempPesanMasuk.length; i++)
        teks_lengkap.add(tempPesanMasuk[i].trim());

    for(int i = 0; i < tempPesanMasuk.length ; i++)
        teksJadi = teksJadi + teks_lengkap.get(i) + " ";

    System.out.println(teksJadi);
}