Wednesday, October 7, 2020

#345 Write a recursive method removeMiddle

Write a recursive method removeMiddle - Computer Science

ChemistryExplain daily providing Q&A content “#345 Write a recursive method removeMiddle" in Computer science, Ba computer science, Berkeley computer science, Computer science associate degree jobs

ChemistryExplain “#345 Write a recursive method removeMiddle in Computer science, Ba computer science, Berkeley computer science, Computer science associate degree jobs
Get the Free Online Chemistry Q&A Questions And Answers with explain. To crack any examinations and Interview tests these Chemistry Questions And Answers are very useful. Here we have uploaded the Free Online Chemistry Questions. Here we are also given the all chemistry topic.

 ChemistryExplain team has covered all Topics related to inorganic, organic, physical chemistry, and others So, Prepare these Chemistry Questions and Answers with Explanation Pdf.

For More Chegg Questions

Free Chegg Question

Assignment: Write a recursive method removeMiddle that receives an array list which has odd number of elements, then it deletes the element in the middle only.

Note: The method should return the middle element that will be removed. The method should receive only one parameter (the array list that is of generic type).

For More Chemistry Notes and Helpful Content Subscribe Our YouTube Chanel - Chemistry Explain  

Free Chegg Answer

// It is assumed that the size of ArrayList is odd.
//Since the size of ArrayList is odd if we continuously remove the first and the last element from ArrayList, 
we will eventually end up with the middle element. // [1, 2 , 3, 4, 5] --> [2, 3, 4] --> [3] (just return the last element) //Before returning, remove it then return it. static <T> T removeMiddle(ArrayList<T> arrayList){ if(arrayList.size() == 1) return arrayList.get(0); T ele = removeMiddle(new ArrayList<T>(arrayList.subList(1, arrayList.size()-1))); arrayList.remove((T)ele); return ele; }

Labels: , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home