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
Step: 1
This method iterates through the elements in the ArrayList calculates the distance between each elem...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started