-5

how can i change out the for loop in this program:

   public class deleteplz{
  public static void main(String args[]){
    String[] cdSporNavn = new String[4];
    cdSporNavn[0]="fritjof i fjøset";
    cdSporNavn[1]="java jive";
    cdSporNavn[2]="hæla i taket";
    cdSporNavn[3]=cdSporNavn[1] + "på julebord";

    for (int sporNr=0; sporNr<cdSporNavn.length; sporNr++){
      if(cdSporNavn[sporNr].indexOf("java") != -1){
        System.out.println(cdSporNavn[sporNr]);
      }
    }
  }
}

with the enhanced for loop? thanks in advance

yyzzer1234
  • 153
  • 1
  • 1
  • 7

1 Answers1

1

You can do it using syntax like the following:

for (String s: cdSporNavn){
  if(s.indexOf("java") != -1){
    System.out.println(s);
  }
}

See Using Enhanced For-Loops with Your Classes for further information and examples.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285