Results 1 to 14 of 14
  1. #1
    lulzim is offline Member
    Join Date
    Feb 2011
    Posts
    44
    Rep Power
    0

    Default Need help urgent!!!

    private ArrayList<newspaper> list;

    public void addToAll(news n)
    {
    for(newspaper np : list){
    np.addNews(n);

    }
    }

    As u can see the code above i have an arraylist where i can store the object of class newspaper and im passing an another object in method addToAll() what i want from you guys is only can somebody explain me this code and can i do any another way for for loop not like newspaper np :list but another way.

    I really appreciate that.
    i
    I think u have got my idea.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Don't put urgent in your title, less people will help out. However, this is called a for each loop. Try looking Ito a regular loop to loop through the list.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    I believe you have been told before about marking your threads as urgent.

    I'll get back to you as soon as I feel like it not when you demand it!

  4. #4
    lulzim is offline Member
    Join Date
    Feb 2011
    Posts
    44
    Rep Power
    0

    Default

    Sorry guys for that word urgent.

    Anyway hope u guys will give me any explonation of this code.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I gave an extremely vague answer. The question isn't that challenging. That code uses a special loop called a for:each loop.

    In this example it means, for each newspaper in list, do something.

    You can also use a standard loop which takes the form of
    Java Code:
    for(declaration; condition; step){
      //do something
    }

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Or use a while loop and an Iterator.

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    ^^that too, hope we helped you out.

  8. #8
    lulzim is offline Member
    Join Date
    Feb 2011
    Posts
    44
    Rep Power
    0

    Default

    still not clear :S.

    I knew that that loop it makes to add news in every of object in list but the problem is how can i write in different way this.
    Please I need code for this method not only //do something statements .

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    we try not to give code here, replace where I said //do something with your own code, it's really not very difficult.

  10. #10
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    What kind of code you are expecting, another way of loop or some if/else just give some idea so that your problem can be solved and explain some more
    sanjeev,संजीव

  11. #11
    lulzim is offline Member
    Join Date
    Feb 2011
    Posts
    44
    Rep Power
    0

    Default

    im expecting any another code instead of this one
    for(newspaper np : list){
    np.addNews(n);

    Is there any another method how to do this ???

    I need only the loop statemtn not the block statement;
    If u say use while or for loop this is not help bc i know that there is while and for bu the problem is how to pass an obj like im passing here in for loop.

    i think u got it guys.

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    No, I don't really understand it, there is no way(besides loops) to traverse an unknown sized list and perform some operation at each step.

  13. #13
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    You can do it :
    1. Using Iterator
    2. Normal for loop
    3. Do/ while loop

    Or you do not want to use these
    sanjeev,संजीव

  14. #14
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I suppose you could enforce that the list is a set size, and manually hard code the changes, however; this would be an absolutely TERRIBLE(with lots and lots of emphasis on how bad of a way it is) way to do this.

    I am also hesitant to suggest this way in fear that you won't realize just how wrong it is and implement this way.

    This is basically what loops make easy, since you are doing a repetitive task, using some form of loop(while, do while, or for) is the greatest way to do this.

Similar Threads

  1. [URGENT]SHA Encryption System...need urgent helps
    By java_idiot in forum New To Java
    Replies: 6
    Last Post: 05-02-2010, 11:04 AM
  2. Help Urgent pls pls pls!!!!
    By jammyjamsticy in forum Java 2D
    Replies: 5
    Last Post: 05-06-2009, 11:55 PM
  3. Urgent Help!!
    By Winniee in forum New To Java
    Replies: 5
    Last Post: 02-17-2009, 04:32 AM
  4. Hi, need some urgent help!
    By jdark in forum New To Java
    Replies: 2
    Last Post: 04-18-2008, 07:50 AM
  5. Help me ...urgent!
    By googgoo in forum New To Java
    Replies: 7
    Last Post: 04-05-2008, 09:46 AM

Posting Permissions

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