0

I have the following enhanced for loop and I don't understand how it works. Can someone explain this particular example to me?

@Path("vm")
public class VmResource {

    @GET
    @Produces("application/json")
    public List<HostDetail> getVms() {
        VirtualBoxManager manager = VirtualBoxManager.createInstance(null);

        List<HostDetail> machineValues = new ArrayList<HostDetail>();

        for (String hostUri : PropertiesBean.getInstance().getServices()) {
            try {
                manager.connect(hostUri, PropertiesBean.getInstance().getUsername(), PropertiesBean.getInstance().getPassword());
                IVirtualBox vbox = manager.getVBox();
                IHost host = vbox.getHost();

                URL url = null;
                try {
                    url = new URL(hostUri);
                } catch (MalformedURLException ex) {
                    Logger.getLogger(VmResource.class.getName()).log(Level.SEVERE, null, ex);
                }

                HostDetail hostDetail = createHostDetail(host, url.toString(), 
                        vbox.getVersion(), url.getHost());
                for (IMachine machine : vbox.getMachines()) {
                    hostDetail.getVirtualMachines().add(createMachineDetail(machine));
                }
                machineValues.add(hostDetail);
            } catch (Exception ex) {

            } finally {
                manager.disconnect();
            }
        }
        return machineValues;
    }
Samuel Edwin Ward
  • 6,526
  • 3
  • 34
  • 62
DCodes
  • 1
  • 2

1 Answers1

0

The for-each loop example,

for (String hostUri : PropertiesBean.getInstance().getServices()) {

acts something like

Iterator<String> iter = PropertiesBean.getInstance().getServices();
for (; iter.hasNext();) {
    String hostUri = iter.next();

and / or if getServices() returns an array then something like

String[] arr = PropertiesBean.getInstance().getServices();
for (int i = 0; i < arr.length; i++) {
    String hostUri = arr[i];
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249