If you have a Java array as follows:
double myarr[10];
You access elements in an array by index (assuming the array has been populated with data)
double somenum = myarr[3]; // extracts the *fourth* element from the list
To set a value in the array, you use the assignment operator and specify a value:
myarr[7] = 3.14159; // sets the *eighth* element to value '3.14159'
If you wish to iterate over a range of numbers, you can use a for-loop. For-loops have the following format:
for (initialization; condition; increase)
If you wanted to print all numbers between 1 and 10, you can write:
for (int i=1; i<=10; i++) {
System.out.println(i);
}
The trick is to use the variable i
in the for-loop and ensure the loop iterates over the proper range. Hint: You can use i
as an array index.
Here are some good resources: