+ 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;

16th Apr 2018, 11:34 AM
ganga
3 Réponses
+ 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.
16th Apr 2018, 12:09 PM
LunarCoffee
LunarCoffee - avatar
16th Apr 2018, 12:05 PM
John Wells
John Wells - avatar
+ 1
Thanx sir
18th Apr 2018, 5:15 PM
ganga