Results 1 to 3 of 3

Thread: for..iterator

  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default for..iterator

    Consider the following code snippet:

    employee[] staff = new Employee[100];

    //assume a procedure that retrieves 100 names from a data source
    retrieveEmployees(staff);

    //cost cutting time...reduce salaries by 20 percent
    for (employee e : staff)
    reducesalaryBy20percent(staff);
    Does the for loop create a copy of the staff array in order to iterate through each staff member?

    :confused:

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,522
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by jon80 View Post
    Does the for loop create a copy of the staff array in order to iterate through each staff member?
    No it doesn't, it iterates over the original array. What is going wrong then? Also show us the definition of your reducesalaryBy20percent( ... ) method; you call it as if it takes an entire array of Employees but I guess it should just take a single Employee ...

    kind regards,

    Jos
    Last edited by JosAH; 11-28-2010 at 02:14 PM.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by jon80 View Post
    CDoes the for loop create a copy of the staff array in order to iterate through each staff member?
    The JLS states what the for-each loop does: JLS for-each

    I don't see it making a copy of the iterable, but rather it extracts the value from each item in the iterable.

Similar Threads

  1. Replies: 0
    Last Post: 05-18-2010, 12:53 AM
  2. Iterator help
    By alpdog14 in forum New To Java
    Replies: 2
    Last Post: 10-13-2009, 08:42 PM
  3. iterator method help..
    By mayhewj7 in forum New To Java
    Replies: 1
    Last Post: 04-08-2009, 03:45 AM
  4. iterator
    By venkatallu in forum Advanced Java
    Replies: 3
    Last Post: 09-23-2008, 01:32 PM
  5. Iterator
    By eva in forum New To Java
    Replies: 0
    Last Post: 01-31-2008, 02:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •