-
how to read array index
Hello,
I need to read an array index but I am getting the value within the array index:
Code:
int diff = 0;
int lowest = 0;
int ans =200;
int position = 0;
int array1[] = {1,200,8,78,99};
for(int i = 0; i < 4; i++){
diff = Math.abs(array1[i] - array1[i + 1]);
if(ans > diff){
ans = diff;
position = array1[i];
}
I need to store the array index into the variable position
-
What is your goal exactly? In the array
Code:
int array1[] = {1,200,8,78,99};
The indices go 0,1,2,3,4
-
Yes I want to read the index and not the value within the index
-
i is the index in your for loop.
-
but when I code:
position = array1[i];
the value within the index is stored in the variable position and not the index itself
-
you are right got it I just need to write i :)