develop a method findClosestElement that takes an ArrayList of integers list and an int num as parameters.
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 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 */ }
Building Java Programs A Back To Basics Approach
ISBN: 9780135471944
5th Edition
Authors: Stuart Reges, Marty Stepp