Loading...
Monday, September 10, 2018

Java 10 Arrays with local variable Var and Different ways to iterate


Java10ArrayWithLocalVar.java
 package com.java9r;

public class Java10ArrayWithLocalVar {

public static void main(String[] args) {
String[] products = new String[8];
products[0] = "product 1";
products[1] = "product 2";
products[2] = "product 3";
products[3] = "product 4";
products[4] = "product 5";
products[5] = "product 6";
products[6] = "product 7";
products[7] = "product 8";

System.out.println("Products List using enhanced for loop");
for (var product : products) {
System.out.println(product);
}

System.out.println("Products List using for loop ");
for (var i = 0; i < products.length; i++) {
System.out.println(products[i]);
}

System.out.println("Iterate list using while loop ");
var i = 0;

while (i < products.length) {

System.out.println(products[i]);
i++;
}

}
}


Output
Products List using enhanced for loop
product 1
product 2
product 3
product 4
product 5
product 6
product 7
product 8
Products List using for loop
product 1
product 2
product 3
product 4
product 5
product 6
product 7
product 8
Iterate list using while loop
product 1
product 2
product 3
product 4
product 5
product 6
product 7
product 8


0 comments:

Post a Comment

 
TOP