0

A bit of advice needed here, I am trying to code Create a String Array containing any six first names. Use an enhanced for loop to print each name in the array Here is what I have:

public class names {
   private static String[] arrayString;
   private static String[] names;

   /**
    * @param args the command line arguments
    */
   public static void main(String[] args) {               
      String[][]firstnames={
        {"John", "Mary", "Harry", "Ray", "Sean", "Matthew" },
      };

      for (int row=0;row<firstnames.length;row++){
         for(int col=0; col<firstnames[row].length; col++){
             System.out.print(firstnames[row][col]+ " ");
             System.out.println();
         }
      }                
   }    
}

I am being told that you have used fixed values for the conditions on the loops instead of the lengths of the array.

It is better to use the length of the array for maintenance etc

I have spent a long time trying to figure out where I have gone wrong, as far as I can see I have answered the question. Can someone point me in the right direction?

M A
  • 71,713
  • 13
  • 134
  • 174
Ronan2505
  • 19
  • 1
  • 3
  • 10
  • 2
    _I am being told that you have used fixed values for the conditions on the loops instead of the lengths of the array_ whoever told you this is lying – jmj Jul 11 '14 at 15:48
  • Do you have any error message being thrown or you are just asking if you should use fixed values ? – Simon Jul 11 '14 at 15:49
  • 1
    I think what they're trying to tell you is that is not an enhanced for loop. https://blogs.oracle.com/CoreJavaTechTips/entry/using_enhanced_for_loops_with – Bill the Lizard Jul 11 '14 at 15:50
  • No error message, prints out fine, I dont understand what they mean. – Ronan2505 Jul 11 '14 at 16:06

1 Answers1

2

If I understand your question, then I think you wanted to do something with an enhanced foreach loop like this,

String[] firstNames = {"John", "Mary", "Harry", "Ray", "Sean", "Matthew" };
for (String name : firstNames) {
  System.out.println(name);
}

Per the javadoc link,

When you see the colon (:) read it as “in.” The loop above reads as “for each TimerTask t in c.” As you can see, the for-each construct combines beautifully with generics. It preserves all of the type safety, while removing the remaining clutter. Because you don't have to declare the iterator, you don't have to provide a generic declaration for it. (The compiler does this for you behind your back, but you need not concern yourself with it.)

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • he has a 2d Array. So its more something like that `for(String[] names: firstNames) for(String name: names) System.out.println(name);` – gkrls Jul 11 '14 at 15:58
  • @xgeorgekx Perhaps. But when I think firstNames I don't think 2d array. Maybe names as a 2d array, first and last.... but firstNames should be a 1d array (and I made it so in my code). – Elliott Frisch Jul 11 '14 at 15:59
  • Well yeah you are right. I agree with you. It's just that on the code he posted he has a 2d Array for first names. And its not like he explained what he is actually trying to do with the first names array. Anyway you got my vote :P – gkrls Jul 11 '14 at 16:03