java - Remove one blank cell before every filled cell in a string array -
i using code :
arraylist<string> getques = db.getsolutiondata(levelno, level); string str = getques.get(0); stringarr = str.split(""); system.out.println(arrays.tostring(stringarr));
this result output:
07-23 16:10:37.031: i/system.out(2548): [, c, , , d, , , f, , i, , , , a, , f, , a, , , , , , , , , , , b, , c, , d, , , h, , , , , , c, , i, , g, , , a, , , e, , , b, , i, , , , , f, , g, , , h, , , a, , , e, , d, , i, , , , , , f, , , i, , g, , e, , , , , , , , , , , d, , b, , a, , , , h, , b, , , g, , , f]
now want remove 1 blank cell before every cell filled. eg : before second cell there blank cell want remove, before ,d,
there 2 blank cells remove 1 those, before i
1 blank cell, before a
, 3 blank cells remove 1 of them, etc. came conclusion remove 1 blank cell before every filled cell.
i have used :
arraylist<string> list = new arraylist<string>(); (string s : stringarr) if (!s.equals("")) list.add(s); stringarr = list.toarray(new string[list.size()]);
but removes blank cells. kindly help, thankx in advance
// original array list<string> list = arrays.aslist(stringarr); // modified array list<string> l = new arraylist<string>(); // insertion index int index = 0; for(int =0 ; i< list.size() ;i++){ if(!list.get(i).equals(" ") && !list.get(i).equals("")){ index++; //remove empty space end of modified array if(l.size() > 1){ l.remove(i - index); } } //make sure first index not empty if(i - index == -1 ) { index--; } l.add(i - index, list.get(i)); } system.out.println(arrays.aslist(list)); system.out.println(arrays.aslist(l));
result somthing :
[[ c, , , d, , , f, , i, , , , a, , f, , a, , , , , , , , , , , b, , c, , d, , , h, , , , , , c, , i, , g, , , a, , , e, , , b, , i, , , , , f, , g, , , h, , , a, , , e, , d, , i, , , , , , f, , , i, , g, , e, , , , , , , , , , , d, , b, , a, , , , h, , b, , , g, , , f]] [[ c, , d, , f, i, , , a, f, a, , , , , , , , , , b, c, d, , h, , , , , c, i, g, , a, , e, , b, i, , , , f, g, , h, , a, , e, d, i, , , , , f, , i, g, e, , , , , , , , , , d, b, a, , , h, b, , g, , f]]
make sure it's blank or space cell :
if(!list.get(i).equals(" ") && !list.get(i).equals("")){
i think work fine.
Comments
Post a Comment