Question: develop a method findClosestElement that takes an ArrayList of integers list and an int num as parameters. The method returns the index of the element
develop a method findClosestElement that takes an ArrayList of integers list and an int num as parameters. The method returns the index of the element in list that is closest to num and is greater than or equal to num. If all elements are smaller than num then the method should return -1.
For example, given list storing values [20, 30, 25, 89, 14],
findClosestElement(list, 26) should return 1 (index of element 30)
findClosestElement(list, 50) should return 3 (index of element 89)
findClosestElement(list, 90) should return -1 (no elements meet the conditions)
// Returns the index of the element in list that is closest to num and is >= num. // Returns -1 if no elements meet the conditions. public static int findClosestElement(ArrayList list, int num) { /* ADD your code here */ }
Step by Step Solution
There are 3 Steps involved in it
This method iterates through the elements in the ArrayList calculates the distance between each elem... View full answer
Get step-by-step solutions from verified subject matter experts
