Provide an example of using a for loop without an array.
Answer)
It is with the help of the for-each loop one can use the loop without the array:
The syntax is as follows:
for (String tempStr : dataOfArray) {
System.out.println(tempStr);
}
For example as below:
class ConventionalApproach{
public static void main(String[] args) {
String [ ] dataOfArray = {"One" , "Two" , "Three", "Four"};
System.out.println("Use of the conventional approach for loop and array");
for (int a = 0; a < dataOfArray.length; a ++){
System.out.println(dataOfArray[a]);
}
//With the use of for each loop// System.out.println("With the help of foreach loop"); for (String tempStr : dataOfArray){ System.out.println(tempStr); }
}
}
Get Answers For Free
Most questions answered within 1 hours.