Reputation: 109
I'm trying to get the minimum and maximum value of an array. The problem is that I'm not sure what exactly happens in the for loop
. We create a for loop
with an int
. Then comes the if statement and also my question: What should numbers[i]
mean? I thought that numbers
already has specified size (9, by initializing { 1,2,3,...,9}). How can we change that to [i]
and what does that i stand for?
public class Array {
public static void main(String[] args) {
int numbers[] = { 92, -108, 2, 120, 67, 4, -81, 9, 88, 1 };
int min, max;
min = max = numbers[0];
for (int i = 1; i < 10; i++) {
if (numbers[i] < min)
min = numbers[i];
if(numbers[i] > max){
max = numbers[i];
}
}
System.out.println("min is: " + min + "; max is: " + max);
}
}
Looking forward to your replies
Upvotes: 0
Views: 7013
Reputation: 930
Lets take the barebones of what you are trying to understand
int numbers[] = { 92, -108, 2, 120, 67, 4, -81, 9, 88, 1 };
int min, max;
min = max = numbers[0];
for (int i = 1; i < 10; i++) {
if (numbers[i] < min)
min = numbers[i];
if(numbers[i] > max){
max = numbers[i];
}
}
First, you are initializing an array of numbers to whatever you have within the {}
. So you have an array that is of length 10. Then we declare two int
's min and max. The values are as such:
numbers[0] = 92;
numbers[1] = -108;
numbers[2] = 2;
numbers[3] = 120;
numbers[4] = 67;
numbers[5] = 4;
numbers[6] = -81;
numbers[7] = 9;
numbers[8] = 88;
numbers[9] = 1;
The next statement is declaring min
and max
as the very first element of the array (0 is the first, all the way up to size-1 for arrays, which in this case is 9).
min = numbers[0] (92).
max = numbers[0] (92).
Next part is the for loop, which is where all the calculations come into play. The for loop is saying start an int
named i
off at 1. Everytime you go through this loop, compare the boolean value in the middle, which in this case is i < 10
which means as long as i
is less than 10, stay in the loop. After each iteration, do the third operation which in this case is i++
, which means increment i
variable by 1.
Great, we are going through the for loop. Lets start off with the first iteration, which means i
is 1. We are taking the SECOND element of the numbers
array (0 is the first element, remember?) and comparing that with the current value of min
first (which is the first element of the array, 92).
Well if you look at the values, -108, which is the second element in the array, is less than min. Therefore, min
is set to the first element in the array now. Next, we compare -108 and see if it is greater than max
, which again is 92. -108 is NOT greater than 92, so it skips that if statement.
i = 1
min = numbers[1] (-108).
max = numbers[0] (92).
Lets go through the second iteration of the for loop. Since we got to the end, we do the i++
statement. Now we are at i
= 2. Lets check to make sure we dont exit. Is the value of i
less than 10? Yes so lets continue from the top!
Is numbers[2] (which is 2) less than the current value of min
(-108)? No, skip that if statement. is numbers[2] greater than the current value of max (92)? No, lets take another look at the values:
i = 2
min = numbers[1] (-108)
max = numbers[0] (92)
Next, since we are at the bottom, do i++
. i
now equals 3.
This continues all the way through till i
= 9, because when i
= 10, it exits out of the for loop due to that middle statement in the for loop, the exit clause.
Upvotes: 0
Reputation:
If u want get min and max value from array, you can use sort() method from java.util Arrays class.
import java.util.Arrays;
public class TestClass {
public static void main(String[] args) {
int[] numbers = { 92, -108, 2, 120, 67, 4, -81, 9, 88, 1 };
// print your array (no sorted)
System.out.println(Arrays.toString(numbers));
Arrays.sort(numbers);
//print your array (sorted)
System.out.println(Arrays.toString(numbers));
for(int i = 0; i<numbers.length;i++){
System.out.println("My array: ["+i+"] " +numbers[i]);
// min = numbers[0] = -108
// My array: [0] -108
// My array: [1] -81
// My array: [2] 1
// My array: [3] 2
// My array: [4] 4
// My array: [5] 9
// My array: [6] 67
// My array: [7] 88
// My array: [8] 92
// My array: [9] 120
// max = numbers[9] = 120
}
}
}
Upvotes: 0
Reputation: 65
As Andrew Rueckert mentioned, in Java, as well as in most other major languages, array[i]
notation provides access to ith element of array array
. You may use square brackets []
to specify the size of an array in its definition, for example:
int[] array = new int[10];
But later on, you use brackets to specify to which element of the array you want to have access to. In your example, there is for
loop iterating from 1 to 10. In each iteration it reads current (ith) element from the array numbers
.
Upvotes: 1
Reputation: 5215
numbers[i]
is how you access the ith element in the array.
Upvotes: 2