This Java program demonstrates the creation and usage of an array. It initializes a double array myList with 4 values and uses a for loop to iterate over the array elements. During each loop iteration, the program prints the value at the current index.
The key components are:
double[] myList = {3.9, 5.9, 22.4, 31.5};i.System.out.println().public class ArrayExample {
public static void main(String[] args) {
double[] myList = {3.9, 5.9, 22.4, 31.5};
// Print all the array elements
for (int i = 0; i < myList.length; i++) {
System.out.println(myList[i] + " ");
}
}
}
3.9 5.9 22.4 31.5 Press any key to continue . . .
First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.