Results 1 to 19 of 19
  1. #1
    sdavis19 is offline Member
    Join Date
    Jan 2017
    Posts
    4
    Rep Power
    0

    Default What is the equivalent of this tradition for loop to a for each loop?

    I have a tradition for loop below and I just wondering what the equivalent loop would be for each loop.

    for (int i = 0; i < words.size(); i++)
    {
    words.set(i, words.get(i).toUpperCase());
    }
    Last edited by sdavis19; 01-26-2017 at 02:17 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: What is the equivalent of this tradition for loop to a for each loop?

    What is the definition for word and words?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    sdavis19 is offline Member
    Join Date
    Jan 2017
    Posts
    4
    Rep Power
    0

    Default Re: What is the equivalent of this tradition for loop to a for each loop?

    Word was a typo and Words is an arrayList of type String

  4. #4
    sdavis19 is offline Member
    Join Date
    Jan 2017
    Posts
    4
    Rep Power
    0

    Default Re: What is the equivalent of this tradition for loop to a for each loop?

    Word is a typo and Words is an arraylist of type String

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: What is the equivalent of this tradition for loop to a for each loop?

    The for each loop doesn't have the i index used in the set() method call.

    See the tutorial: http://docs.oracle.com/javase/tutori...bolts/for.html
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    sdavis19 is offline Member
    Join Date
    Jan 2017
    Posts
    4
    Rep Power
    0

    Default Re: What is the equivalent of this tradition for loop to a for each loop?

    Do you mind giving me an example

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: What is the equivalent of this tradition for loop to a for each loop?

    Do you mind giving me an example
    An example of what?

    Did you read the tutorial at the link? There are examples of for each loops there
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    gozzy is offline Member
    Join Date
    Jan 2017
    Posts
    46
    Rep Power
    0

    Default Re: What is the equivalent of this tradition for loop to a for each loop?

    It's not possible to use a for-each loop. Unless you are to provide a variable to use as an index (so you might as well use a normal loop)

  9. #9
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: What is the equivalent of this tradition for loop to a for each loop?

    This is what you want?

    Java Code:
    class EnhancedForDemo {
        public static void main(String[] args){
             String[] wordList = {"tuna", "ham", "cheese", "pizza", "potato"};
             for (String singleItem : wordList) {
                 System.out.println("Capitalized: " + singleItem.toUpperCase());
             }
        }
    }

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: What is the equivalent of this tradition for loop to a for each loop?

    @cronnin Please don't just provide code and spoonfeed the OP. Try to help them work through their problem.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: What is the equivalent of this tradition for loop to a for each loop?

    Oh, sorry. Gotcha.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: What is the equivalent of this tradition for loop to a for each loop?

    No problem. Enthusiastic new members are always welcome.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    gozzy is offline Member
    Join Date
    Jan 2017
    Posts
    46
    Rep Power
    0

    Default Re: What is the equivalent of this tradition for loop to a for each loop?

    This is what you want?

  14. #14
    gozzy is offline Member
    Join Date
    Jan 2017
    Posts
    46
    Rep Power
    0

    Default Re: What is the equivalent of this tradition for loop to a for each loop?

    This is what you want?
    Well that's in no way equivalent is it? A List is not being used, the elements of the List aren't being set ..

  15. #15
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: What is the equivalent of this tradition for loop to a for each loop?

    Oh, I see, you want to change date while iterating though it? There is a gimmick to make it work. You iterate though a copy/clone, and change data in the original.
    Last edited by cronnin; 01-28-2017 at 08:55 PM.

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: What is the equivalent of this tradition for loop to a for each loop?

    Consider a list of Strings call names.

    Java Code:
    for (String n : names) {
        n.toUpperCase();
    }
    that just changes the case. It does not replace the String

    The rule is that you can use the object reference of the for/each construct to call
    methods and use that to change fields. But you can't modify the location in the list where
    the reference resides.

    And if you try to change the list with an index you might get a concurrent modification exception.
    I believe the OP is doing it the best way other than creating a new list on the fly. In that case
    the OP could employ streams (java 1.8+)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  17. #17
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: What is the equivalent of this tradition for loop to a for each loop?

    I was thinking like this:


    Java Code:
    import java.util.ArrayList;
    
    public class EnhancedForDemo {
        public static void main(String[] args){
             ArrayList<String> foodList = new ArrayList<>();
             foodList.add("one");
             foodList.add("two");
             foodList.add("three");
             foodList.add("four");
             foodList.add("five");
    
            for (String singleItem :  new ArrayList<String>(foodList)) {
                    foodList.add(singleItem.toUpperCase());
                    foodList.remove(singleItem);
    
                    // the rest is just to see what's going on
                    for(String singleItem2 : foodList){
                         System.out.println(singleItem2);
                    }
    
                    System.out.println("");
             }
               
          
         } 
    }

  18. #18
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: What is the equivalent of this tradition for loop to a for each loop?

    Well, that certainly works. I was focusing on doing a direct replace of the object.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  19. #19
    cronnin is offline Member
    Join Date
    Jan 2017
    Posts
    38
    Rep Power
    0

    Default Re: What is the equivalent of this tradition for loop to a for each loop?

    Agree. Seems not possible. Maybe just mask it better by changing lines 13 an 14 with:

    Java Code:
    foodList.add(foodList.indexOf(singleItem), singleItem.toUpperCase());
    foodList.remove(singleItem);
    That's like when your keyboard button = is broken and you can't type in the for loop :)
    Last edited by cronnin; 01-31-2017 at 02:37 AM.

Similar Threads

  1. Simple loop within a loop not working.
    By supremegrandruler in forum New To Java
    Replies: 3
    Last Post: 03-21-2014, 09:27 AM
  2. What is equivalent for loop?
    By diamonddragon in forum New To Java
    Replies: 5
    Last Post: 01-15-2012, 09:16 AM
  3. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  4. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  5. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM

Tags for this Thread

Posting Permissions

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