Question: Java sortArrayList Code a method called sortArrayList, that takes an ArrayList of Integers as a parameter and returns another ArrayList of the same size. The
Java
sortArrayList
Code a method called sortArrayList, that takes an ArrayList of Integers as a parameter and returns another ArrayList of the same size. The method is to sort the elements in the ArrayList in descending order.
Assume the ArrayList always has more than two elements.
Example 1
ArrayList: {12, 80} return: {80, 12 }
Example 2
ArrayList: {12, 80, 109, -2} return: {109, 80, 12, -2 }
getPairWithClosestToN
Code a method called getPairWithClosestToN, that takes an array of integers and an integer, n as parameters and prints out the pair of elements in the array that have the summation closest to n. The pairs are formed by first and second elements, third and fourth elements, and so on. Assume that all elements are positive numbers.
Your code must print out a warning statement if the length of the array is smaller than 2, or is an odd number.
Example 1
array: {1, 8, 2, 4, 5, 2}, n:5
Print out: Pair that have sum closest to 5 is 2 and 4.
Example 2
array: {2}, n:5
Print out: Invalid input.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
