Generalizing an iterative function over a collection of Objects in Java -
i have generalize repeating , repeating code single method possible ?
//attrrischio sagtblobjattrischioang sagtblobjattrischioang = elemtout.getattrischio(); if(sagtblobjattrischioang.length()>0) { (int j = 0; j <sagtblobjattrischioang.length(); j++) { sagobjattrischioang item = sagtblobjattrischioang.getelement(j); system.out.println(tostringentities.tostring(item)); } } // beni sagtblobjbeniang sagtblobjbeniang = elemtout.getbeni(); if(sagtblobjbeniang.length()>0) { (int j = 0; j <sagtblobjbeniang.length(); j++) { sagobjbeniang item = sagtblobjbeniang.getelement(j); system.out.println(tostringentities.tostring(item)); } } and on , on again
both sagtblobjattrischioang , sagtblobjbeniang implement common interface (e.g sagtabobj , implement method length , getelement return common interface sabtab.
then create common method
public void printitems(sabtabobj item){ if(item.length()>0) { (int j = 0; j < item.length(); j++) { sagtab sabtab = item.getelement(j); system.out.println(tostringentities.tostring(sabtab)); } } }
Comments
Post a Comment