user3233280
user3233280

Reputation: 291

How to Split an ArrayList in Android Using JAVA?

Here is the code to put values in ArrayList and I am unable to split the arraylist with ",". Can someone please help me as to how to achieve this task ?

           spinnerArrayList = new ArrayList<String>();
           spinnerArrayList.add(menuFieldInstance.getFieldValues());
           Log.i("spinnerArrayList",""+spinnerArrayList);  
          //for(int j=0;j<spinnerArrayList.size();j++)
          //{
          Log.i("spinnerArrayList after splitting ,",""+spinnerArrayList.get(0).split(","));
           //} 

Here is the Logcat of Spinner ArrayList and SpinnerArrayList after splitting.............

02-10 22:00:48.285: I/spinnerArrayList(19378): [0100~Avon & Somerset,0200~Bedfordshire,0300~Cambridgeshire,0400~Cheshire,0500~City of London,0600~Cleveland,0700~Cumbria,0800~Derbyshire,0900~Devon & Cornwall,1000~Dorset,1100~Durham,1200~Essex,1300~Gloucestershire,1400~Greater Manchester,1500~Hampshire,1600~Hertfordshire,1700~Humberside,1800~Kent,1900~Lancashire,2000~Leicestershire,2100~Linconshire,2200~Merseyside,2300~Metropolitan,2400~Norfolk,2500~Northamptonshire,2600~Northumbria,2700~North Yorkshire,2800~Nottinghamshire,2900~South Yorkshire,3000~Staffordshire,3100~Suffolk,3200~Surrey,3300~Sussex,3400~Thames Valley,3500~Warwickshire,3600~West Mercia,3700~West Midlands,3800~West Yorkshire,3900~Wiltshire,4000~Dyfed,4100~Gwent,4200~North Wales,4300~South Wales,4400~Royal Ulster,4500~Strathclyde,4600~Central Scotland,4700~Dumfries and Galloway,4800~Fife,4900~Grampian,5000~Lothian and Borders,5100~Northern Scotland,5200~Tayside,5300~Gurnsey,5400~States of Jersey,5500~Isle of Man,NO~No Police Response,THAM~THAMES VALLEY,WEST~WEST MIDLANDS POLICE,5600~Buckinghamshire] 02-10 22:00:48.285: I/spinnerArrayList after splitting ,(19378): [Ljava.lang.String;@41b9a498

Upvotes: 0

Views: 6854

Answers (3)

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

// try to print this way then you getting actual value at index becz your try to print String[] object rather each index value so do this way
spinnerArrayList = new ArrayList<String>();
spinnerArrayList.add("");
for (int i=0;i<spinnerArrayList.size();i++){
        String[] splitedValue = spinnerArrayList.get(i).split(",");
        for (int j=0;j<splitedValue.length;j++){
            Log.i(i+" at ArrayIndex "+j+" at splitedIndex Value is >> ",splitedValue[j]);
            String[] splitedValue1 = splitedValue[j].split("~");
            if(splitedValue1.length==1){
                continue;
            }
            for (int k=0;k<splitedValue1.length;k++){
                Log.i(j+" at splitedIndex "+k+" at splited1Index Value is >> ",splitedValue1[k]);
            }
        }

    }

Upvotes: 2

ikos23
ikos23

Reputation: 5354

You can't split ArrayList. You can split String, e.g "I am some String, you can split me".split(",") will return an array of 2 Strings, but ArrayList is a data structure which holds some Strings and it doesn't mean that they are separated with comma. You can try to split each item of the list, e.g.

for (String s : spinnerArrayList) {
    String[] res = s.split(",");
    // do smth with res
}

Upvotes: 1

Nir Alfasi
Nir Alfasi

Reputation: 53535

spinnerArrayList.get(0).split(",") doesn't split "the arrayList" it splits the string at index zero, and since the returned result is an array of strings - you can't concatenate it using the + operator to another string:

Log.i("spinnerArrayList after splitting ,",""+spinnerArrayList.get(0).split(","));

Read the splitted string into a String array first:

String[] arr = spinnerArrayList.get(0).split(",");

and then loop the array and print the values"

for(String val: arr){
    Log.i(val);
}

Upvotes: 0

Related Questions