+ 1
how to execute the below java logic
public static void main(String[] args) { System.out.println(new6(6)[5]); } public static int[] new6(int n){ int[] result=new int[n]; for(int i=0;i<result.length;i++){ result[i]=5000; } return result;
3 Answers
+ 2
General structure of the program:
1) Define the method "new6" with one parameter.
a) Initialize a new array whose size is the parameter.
b) For every element in the array, make it equal to 5000.
c) Return the array.
2) Run the main method.
a) Get result of new6 called with the parameter 6, which is an array, and then get the 6th element of the array.
b) Print that out.
So when we call the method in main (part 2 of the above), we pass 6 in as a parameter. With that in mind, the new6 method:
1) Initializes a new array of size 6 (6 elements total).
2) Makes every value in the array 5000.
3) Returns that new array.
Then, we index the array with the index of 5, and because every element of the array is 5000, the printed value will be 5000.
+ 1
Works fine for me:
https://code.sololearn.com/c115xz7WQWBk
+ 1
Thanx sir