Java Basics - Arrays 2
Arrays and Loops
We can pass arrays through functions.
For example public double calculateAverage(double [] temperatures){
If we look up and index beyond the size of the array we will get the following error: "ArrayIndexOutOfBoundsException"
Therefore it is important to check that the array is not out of bounds.
__________________________________________________________________
How to search for the minimum number in the array:
Lets make the following program:
/*
*Searches an array of speed for the fastest(smallest value)
*@param speed array of speeds
*@return double the fastest speed found
*/
public double search(double [] search){
int size = search.length;
double min=speed[0];
for(int i = 1; i<=size; i++){
if(search[i]<min) min = search[i];
}
return min;
}
________________
How to reverse an array of strings example
public void printInReverse(String[] stringArray) {
for (int i = stringArray.length - 1; i>=0; i--){
System.out.println(stringArray[i]);
}
}
We can pass arrays through functions.
For example public double calculateAverage(double [] temperatures){
If we look up and index beyond the size of the array we will get the following error: "ArrayIndexOutOfBoundsException"
Therefore it is important to check that the array is not out of bounds.
__________________________________________________________________
How to search for the minimum number in the array:
Lets make the following program:
/*
*Searches an array of speed for the fastest(smallest value)
*@param speed array of speeds
*@return double the fastest speed found
*/
public double search(double [] search){
int size = search.length;
double min=speed[0];
for(int i = 1; i<=size; i++){
if(search[i]<min) min = search[i];
}
return min;
}
________________
How to reverse an array of strings example
public void printInReverse(String[] stringArray) {
for (int i = stringArray.length - 1; i>=0; i--){
System.out.println(stringArray[i]);
}
}
Comments
Post a Comment