+ 1

How the output is 00301?

Java arrays https://code.sololearn.com/ceijNeDy5IoG/?ref=app

3rd Oct 2021, 5:46 AM
alagammai uma
alagammai uma - avatar
12 Answers
+ 3
alagammai uma , Because you used a[n] = 0 Instead of n = 0 In range-based for loops (int n : a) every iteration n gets assigned to the corresponding value in the array a. So there is no need for [] here, as n will be equal to the next value in the array every iteration...
3rd Oct 2021, 6:10 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 3
alagammai uma for(int n : a) This is called enhanced for loop and here n will be each value of an array a But when you do a[n] then here n will work as index For the array {1, 2, 3, 4, 1} n = 1 a[n] = 0 means a[1] = 0 Now array will be {1, 0, 3, 4, 1} Now on next iteration n = 0 so a[0] = 0 Now array will be {0, 0, 3, 4, 1} Again on next iteration n = 3 so a[3] = 0 Now array will be {0, 0, 3, 0, 1}
3rd Oct 2021, 6:27 AM
A͢J
A͢J - avatar
+ 2
Ãnjaan , mate read the question again.
3rd Oct 2021, 11:29 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
+ 2
Ãnjaan Don't give answer without reading question.
3rd Oct 2021, 4:30 PM
A͢J
A͢J - avatar
+ 1
No problem mate...
3rd Oct 2021, 6:12 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
3rd Oct 2021, 6:29 AM
alagammai uma
alagammai uma - avatar
+ 1
Ãnjaan , your program neither solves the problem in his code or answers his question.
3rd Oct 2021, 11:09 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
0
There are a few ways to make certain elements of array equal 0 to output 00301.. I included 2 of them in that code: https://code.sololearn.com/cL8fjkA2fNev/?ref=app
3rd Oct 2021, 6:04 AM
Aleksei Radchenkov
Aleksei Radchenkov - avatar
0
Aleksei Radchenkov y in my code not all elements r replaced as 0?
3rd Oct 2021, 6:07 AM
alagammai uma
alagammai uma - avatar
0
Aleksei Radchenkov ok thank you
3rd Oct 2021, 6:11 AM
alagammai uma
alagammai uma - avatar
0
Right answer is here. Check it! public class Program { public static void main(String[] args) { int[] a={1,2,3,4,1}; for(int n:a) { System.out.println(n); } } }
3rd Oct 2021, 11:07 AM
Jigri
Jigri - avatar
0
Thik hai bahi ye It's right
3rd Oct 2021, 11:11 AM
Jigri
Jigri - avatar