Results 1 to 7 of 7
  1. #1
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default Help for Enhanced for loop

    Hi,

    I have a question about how to use an enhanced for loop to search through all the elements of the list in the third class and print some names:
    Java Code:
    Main class:
    
    Public class Factory
    
    { private LinkedList<Car> cars = new LinkedList<Car>();
       ..
       ..
       for (Door door: doors)
       { door.showMaterial(); }
       ..
    }
    
    Second class:
    
    public class Car
    { ..
       private LinkedList<Door> doors = new LinkedList<doors>();
       ..
       ..
    }
    
    Third class:
    
    Public class Door
    { .. 
       ..
       public void showMaterial()
      { . . . ..}
      ..
    }
    I don't know what I really should do to access the showMaterial method in the Door class directly from the main class. I don't want to put the call method in the Car class because when I run the main method of the Factory class it has to start from there. Please help me as I have done a lot of research and studies but couldn't figure it out.

    Thank you all.

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    What is your goal? Are you trying to get all doors from all cars from the Factory object?

    Java Code:
    Main class:
    
    Public class Factory
    
    { private LinkedList<Car> cars = new LinkedList<Car>();
       ..
       ..
       for(Car car : cars)
       {
          for (Door door: car.getDoors())
          { door.showMaterial(); }
       }
       ..
    }
    
    Second class:
    
    public class Car
    { ..
       private LinkedList<Door> doors = new LinkedList<doors>();
       ..
       public LinkedList<Door> getDoors()
       {   return doors;  }
       ..
       ..
    }
    
    Third class:
    
    Public class Door
    { .. 
       ..
       public void showMaterial()
      { . . . ..}
      ..
    }
    My Hobby Project: LegacyClone

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

    Default

    the doors list by itself has no meaning in the Factory class, but it has meaning for each car held in the linked list of cars in the Factory.

    Perhaps you can tell us what are you trying to do with this information and why? This way we can better figure out how to help you.

  4. #4
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default

    What I am trying to do is to show materials for all of the doors, the way you have shown me in the code is showing the material for every car and then every door which gets duplicated and is shown twice.

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

    Default

    You may wish to explain in quite a bit more detail just what you're trying to do here. It's not clear to either mr matt or to me.

  6. #6
    jboy is offline Member
    Join Date
    Aug 2009
    Posts
    56
    Rep Power
    0

    Default

    Ok, If I put a System.out.println(material) in the method showMaterial() in the Door class and say I have a list of two cars, the material will be printed 4 times instead of 2 times because the material will be printed once for each car and once for each door and as we have two cars: 2*2 =4. I hope you understand now..

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

Similar Threads

  1. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM
  2. while loop help
    By kathyla18 in forum New To Java
    Replies: 1
    Last Post: 03-02-2009, 06:49 PM
  3. Enhanced for loop
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-03-2007, 09:24 PM
  4. Enhanced For loop In Java
    By goldhouse in forum Advanced Java
    Replies: 1
    Last Post: 05-06-2007, 04:26 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
  •