android - Delete null element in 2d array in Java -
help me please!, have array of strings:
data2[3][2] = [[datos varios, datos empresa, null], [listado2, listado2 extendido, null], [pendientes validar, pendientes liquidar, liquidados], [pendientes aprobar, pendientes pago, pagados]]
i found resize array obtain this: dont know way..
data2[?][?] = [[datos varios, datos empresa], [listado2, listado2 extendido], [pendientes validar, pendientes liquidar, liquidados], [pendientes aprobar, pendientes pago, pagados]]
thanks in advance !!
i thought problem possibly 2d structure of array, , suggested links 1d. here suggested solution 2d case build simple test on data.
import java.util.arraylist; public class testapp { public static void main(string[] args){ string[][] data = {{"datos varios", "datos empresa", null}, {"listado2", "listado2 extendido", null}, {"pendientes validar", "pendientes liquidar", "liquidados"}, {"pendientes aprobar", "pendientes pago", "pagados"}}; string[][] cleaneddata = removenull(data); system.out.println("before"); printarray(data); system.out.println("after"); printarray( cleaneddata ); } public static void printarray(string[][] arr2d){ for(string[] arr1d: arr2d){ for(string s: arr1d){ system.out.print(s+", "); } system.out.print("\n"); } } public static string[][] removenull( string[][] arr2d) { // arraylist<arraylist<string>> list2d = new arraylist<arraylist<string>>(); // for(string[] arr1d: arr2d){ arraylist<string> list1d = new arraylist<string>(); for(string s: arr1d){ if(s != null && s.length() > 0) { list1d.add(s); } } // possibly not want empty arrays in 2d array // removed them if(list1d.size()>0){ list2d.add(list1d); } } string[][] cleanarr = new string[list2d.size()][]; int next = 0; for(arraylist<string> list1d: list2d){ cleanarr[next++] = list1d.toarray(new string[list1d.size()]); } return cleanarr; } }
Comments
Post a Comment