Hi Could you plaese explain me why is this output int nums

Hi!

Could you plaese explain me why is this output:

int [] nums = {37, 99, 16, 80 };

int y = 1;

nums[y + 1] = nums[y + 2];

System.out.printf(\"%d, %d, %d, %d\", nums[0], nums[1], nums[2], nums[3]);

Output:

37, 99, 80, 80

Solution

import java.util.Scanner;

class Ak
{
public static void main(String args[])
{
int [] nums = {37, 99, 16, 80 };

int y = 1;

nums[y + 1] = nums[y + 2];

System.out.printf(\"%d, %d, %d, %d\", nums[0], nums[1], nums[2], nums[3]);
}
}

Output:

37, 99, 80, 80

Explanation :

In the above one dimensional array we are declaring array elements as
nums[0] = 37, nums[1]=99, nums[2]=16, nums[3]=80
if we want to print the above array elements then the values of nums[0] & nums[1] are not change but while comming to nums[2] value it is equals to nums[3] value
because we have nums[y + 1] = nums[y + 2]; in the program
given value of y =1 then
nums[1 + 1] = nums[1 + 2]; // By substituting 1 in place of y
nums[2] = nums[3]; // Here value of nums[3] is assigned to nums[2]
In the given array value of nums[3] = 80
now the value of nums[2] becomes value of nums[3]
i.e nums[2] = 80

so the output will becomes 37, 99, 80, 80

Hi! Could you plaese explain me why is this output: int [] nums = {37, 99, 16, 80 }; int y = 1; nums[y + 1] = nums[y + 2]; System.out.printf(\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site