Results 1 to 3 of 3
  1. #1
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default How to set up this Class to use FOR-EACH loops?

    Java Code:
    import java.util.*;
    import java.io.Serializable;
    
    public class Customers implements Iterable<Customers.Customer>, Serializable {
    
        public class Customer {
            ...
            //constructor
            Customer(params) {
                ...
                addCustomer(this);
            }
        }
    
        private Set<Customer> customerSet = new HashSet<Customer>();
    
        public void addCustomer(Customer c) {
            customerSet.add(c);
        }
    
        public Iterator<Customer> getCustomers() {
            return customerSet.iterator();
        }
    
        public Iterator<Customer> iterator() {
            return getCustomers();
        }
    
        public Customer getCustomer(int cid) {
            for (Customer c:[COLOR="Red"][U]Customers[/U][/COLOR]) {
                if (c.id == cid) {
                    return c;
                }
            }
        }
        
    }
    NetBeans is giving me the error for Customers (underlined in my code) that "cannot find symbol". Does anyone know what am I doing wrong here? I need to be able to iterate through all instances of Customer :confused:
    Last edited by ozzyman; 04-04-2011 at 11:28 PM.

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Turns out this is the correct usage:

    Java Code:
        public Customer getCustomer(int cid) {
            [COLOR="Red"]Customers myCustomers = new Customers();[/COLOR]
            Customer thisCustomer = new Customer();
            for (Customer c:[COLOR="red"]myCustomers[/COLOR]) {
                if (c.id == cid) {
                    thisCustomer = c;
                    break;
                }
            }
            return thisCustomer;
        }

    Problem solved. Thanks for looking.

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Makes me wonder though, will this mean it will iterate through ALL customers created or just the ones added to "myCustomers" variable?

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2011, 02:17 AM
  2. Problem implementing ActionListener??
    By ryanonnfire54 in forum New To Java
    Replies: 3
    Last Post: 11-16-2010, 05:39 PM
  3. Problem Creating/implementing Objects
    By ramathews in forum New To Java
    Replies: 2
    Last Post: 02-25-2010, 04:42 AM
  4. problem with implementing list
    By timkd127 in forum New To Java
    Replies: 3
    Last Post: 04-13-2009, 04:54 AM
  5. java.util
    By Java Tutorial in forum Java Tutorial
    Replies: 1
    Last Post: 02-07-2008, 02:46 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
  •