7 Replies Latest reply: Sep 1, 2008 3:15 PM by 843785 RSS

    Collection based loop

    843785
      Hi!

      I've been trying write a collection based loop for a program but only been able to use it on an enum. The code it the following
      public class Collection {
      enum Words { one, two, three };
           public static void main(String args[]) 
           {
                for (Words word: Words.values()) 
                {
                     System.out.println(word);
                }          
           }
      }
      But I can't seem to get i to work for arrays. I've done some googling but I haven't found any useful code out there. Is there a way to do this?

      /MJ
        • 1. Re: Collection based loop
          843785
          Words.values() is an array
          • 2. Re: Collection based loop
            843785
            Try this
            public static void main(String[] args) {
                String text = "You know you've achieved perfection in design, Not when you have nothing more to add, But when you have nothing more to take away.";
                String[] words = text.split(" ");
                for(String word: words)
                        System.out.println(word);
            }
            • 3. Re: Collection based loop
              843785
              Sure, but why doesn't the code accept an array as an argument? I can get the enum to be accepted as an argument but not an array. The following code is not accepted and I've not been able to make it work.
              public class Collection {
                   public static void main(String args[]) 
                   {
                        String Words[] = { "one", "two", "three" };     
                        for (Words word: Words.values()) 
                        {          
                             System.out.println(word);
                        }
                   }
              }
              • 4. Re: Collection based loop
                843785
                MidnightJulia wrote:
                Sure, but why doesn't the code accept an array as an argument? I can get the enum to be accepted as an argument but not an array. The following code is not accepted and I've not been able to make it work.
                Why do you want to call .values() on the array? Arrays don't have such a method. Simply write "Words" there.

                Also, you should really follow the naming conventions. At least give your variables lower-case names ("words" instead of "Words"), otherwise it'll get easy to confuse them with types (classes/interfaces/enums/annotations usually start with a capital letter, such as "String", "Integer", "Collection", "MyFirstClass", ...).
                • 5. Re: Collection based loop
                  843785
                  Thank you! That was exactly what I was looking for.
                  • 6. Re: Collection based loop
                    843785
                    Why do you want to call .values() on the array? Arrays don't have such a method. Simply write "Words" there.
                    I'm rewriting an example program (in all ways I can come up with) and I keeped that piece of code. I didn't even think about that :)
                    Also, you should really follow the naming conventions. At least give your variables lower-case names ("words" instead of "Words"), otherwise it'll get easy to confuse them with types (classes/interfaces/enums/annotations usually start with a capital letter, such as "String", "Integer", "Collection", "MyFirstClass", ...).
                    I will :)
                    • 7. Re: Collection based loop
                      843785
                      MidnightJulia wrote:
                      Sure, but why doesn't the code accept an array as an argument? I can get the enum to be accepted as an argument but not an array
                      Slight misunderstanding. You aren't passing the enum anywhere, you're calling the values() method on it, which returns.....an array!

                      Anyways, you seem to have gotten there. Nice one