RaagaSudha
RaagaSudha

Reputation: 397

How to split an ArrayList and store the data into a String array in android?

In my application I am storing string type data in arraylist with "|" operator as separator. i.e my array-list contains data like [|123,124,|,324,543|789,649,666,356] Now I need to split the arraylist with "|" and need to store the data into a string array. How can I do that?

My Code:

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");
        } 
    }
}

It is showing error at if(str[j].contains(",")) line....Please help me regarding this.. thanks in advance...

Upvotes: 1

Views: 9978

Answers (3)

kata sony
kata sony

Reputation: 1

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

Upvotes: 0

user2553189
user2553189

Reputation: 51

Why Don't you simply use this :

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

Upvotes: 1

assylias
assylias

Reputation: 328923

Not sure what you want to achieve, but this could help you to build your code:

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");
                }
            }

        }
    }
}

Upvotes: 1

Related Questions