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

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -