Suggestion to Nested for each loop in Java -
i need ask 1 basic question
public class helloworld{ public static void main(string []args){ int[] x = new int[]{1,2,3,4,5}; int[] z = new int[]{11,22,33,44,55}; for(int y:x){ for(int t: z){ system.out.println(t); break; } system.out.println(y); } } }
output : 11 1 11 2 11 3 11 4 11 5
however , want output 11 1 22 2 33 3 44 4 55 5
what should make output?
thanks help
actually original code this
for(propertyitems propertyitem:propertyitems){ for(address eachaddress: address){ propertyitem.setaddress(eachaddress); eachaddress.setpropertyitems(propertyitem); break; } propertyitemsandaddressdao.savepropertyitems(propertyitem); }
both propertyitems , address set<> . want epropertyitem , eachaddress setted.
this full code
@transactional public set savepropertyitemsservice(set address,set propertyitems, set propertytype,property property) { set savedpropertytypes= new linkedhashset(); propertyandpropertytypeservice.savepropertyservice(propertytype, property);
for(propertyitems propertyitem:propertyitems){ for(address eachaddress: address){ propertyitem.setaddress(eachaddress); eachaddress.setpropertyitems(propertyitem); } propertyitemsandaddressdao.savepropertyitems(propertyitem); } for(propertytype eachpropertytype :propertytype){ eachpropertytype.setpropertyitems(propertyitems); propertytype savedpropertytype = propertytypeandpropertyitemsdao.savepropertytype(eachpropertytype); savedpropertytypes.add(savedpropertytype); } return savedpropertytypes;
}
as @unihedron says:
public static void main(string []args){ int[] x = new int[]{1,2,3,4,5}; int[] z = new int[]{11,22,33,44,55}; for(int i=0; i<x.length; i++){ system.out.print(z[i] + " " + x[i] + " "); } system.out.println(); }
note: @lpratlong suggests, above code works if x.length == z.length
. please treat code example only!
Comments
Post a Comment