Smallest element in array java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Smallest element in array java

I have typed the code to find the smallest element in the array but I have a question The smallest value in the array is 1 and I write a condition using the conditional f statement it says If the first element in the array is less than the smallest element then fulfill the following condition However, the smallest element in the matrix is 1 and the first element in the matrix is also 1 And 1 is not less than 1, but fulfills the condition How can this be ? Please explain to me a detailed explanation https://code.sololearn.com/cP84GC4jrQ0Q

3rd Dec 2020, 4:35 AM
STOP
STOP - avatar
11 Answers
+ 4
public class Program { public static void main(String[] args) { double [] myList= {1,5,4}; double min=myList[0]; int indexOfMin =0; for(int i=0;i<myList.length;i++) if(myList[i]<min) { min=myList[i]; indexOfMin =i;} System.out.print(myList[indexOfMin]); } } Variable indexOfMin is assigned to 0, even if the condition failed variable value is still 0. That's why it's returns the first element of the array Even your condition gets false.
3rd Dec 2020, 4:45 AM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 4
STOP I don't see any problem with your code, works fine. Don't need any edits in your code.
3rd Dec 2020, 4:58 AM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 3
STOP if condition failed than no new value will be assigned to the variable indexOfMin. Variable value is unchanged and remains as you assign it while declaration. and program prints is somewhat similar to myList[0](first one of array).
3rd Dec 2020, 4:52 AM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
0
what do you mean of even if the condition failed variable value is still 0. ? 0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
3rd Dec 2020, 4:48 AM
STOP
STOP - avatar
0
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ can you more explain, and if there any edit to make the code better help me
3rd Dec 2020, 4:52 AM
STOP
STOP - avatar
0
I can’t understand:((((
3rd Dec 2020, 5:07 AM
STOP
STOP - avatar
0
// 0 1 2 double [] myList= {1,5,4}; // 1 double min=myList[0]; for(int i=0;i<myList.length;i++) if(myList[i]<min) { min=myList[i]; min =i;} System.out.print(min); } } I delet indexOfMin. and still gibe me 1 hhhoooowwww
3rd Dec 2020, 5:24 AM
STOP
STOP - avatar
0
STOP your if statement is never executed. The indexOfMin was assigned 0 so even at the end only that is printed so myList[indexOfMin] is myList[0] which is 1.
3rd Dec 2020, 5:25 AM
Avinesh
Avinesh - avatar
0
Avinesh // 0 1 2 double [] myList= {1,5,4}; // 1 double min=myList[0]; for(int i=0;i<myList.length;i++) if(myList[i]<min) { min=myList[i]; min =i;} System.out.print(min); } } I delet indexOfMin. and still give me 1 hhhoooowwww
3rd Dec 2020, 5:27 AM
STOP
STOP - avatar
0
STOP because you already assigned min = myList[0] and printing min will give 1.
3rd Dec 2020, 5:30 AM
Avinesh
Avinesh - avatar
0
ooookkaaaayyy
3rd Dec 2020, 5:31 AM
STOP
STOP - avatar