Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

IN JAVA 6 8 1+ import java.util.ArrayList; 3 40* 5 * Write a method removeBadPairs that accepts an ArrayList of integers and removes any adjacent

IN JAVA

image text in transcribed

6 8 1+ import java.util.ArrayList; 3 40* 5 * Write a method removeBadPairs that accepts an ArrayList of integers and removes any adjacent pair * of integers in the list if the left element of the pair is larger than the right element of the pair. 7 * Every pair's left element is an even-numbered index in the list, and every pair's right element is an odd index in the list. * For example, suppose a variable called list stores the following element values: 9 * [3, 7, 9, 2, 5, 5, 8, 5, 6, 3, 4, 7, 3, 1] 10 * We can think of this list as a sequence of pairs: (3, 7), 19, 2), (5, 5), (8,5), (6, 3), (4,7), (3, 1). 11 * The pairs (9,2), (8,5), (6, 3), and (3, 1) are "bad" because the left element is larger than the right one, so these pairs 12 * should be removed. So the call of removeBadPairs(list); would change the list to store the following element values: 13 * [3, 7, 5, 5, 4, 7] 14 * If the list has an odd length, the last element is not part of a pair and is also considered "bad;" it should 15 * therefore be removed by your method. 16 * If an empty list is passed in, the list should still be empty at the end of the call. 17 * You may assume that the list passed is not null. 18 */ 19 20 21 public class RemoveBadPairs 22 { 23 public static void main(String[] args) 24 25 int[] values = {3, 7, 9, 2, 5, 5, 8, 5, 6, 3, 4, 7, 3, 1); 26 ArrayList list = new ArrayList(); 27 for (int i = 0; i removeBadPairs(ArrayList pairs) { //---- -Start below here. To do: approximate lines of code = 8 1/1. Create a new array list of integers, initially empty 43 44 45 46 47 48 49 50 510 52 53 54 55 2 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 ga 1/2. Check to see if the given array list pairs has an odd number of integer elements // If so, remove the last integer 1/3. use a for loop to go through the given array list pairs // if a pair is "good" add the pair of numbers to the new list // Hint: increment your loop index by 2 and make sure your loop index is

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Visual Basic 6 Databases

Authors: Charles Williams

1st Edition

1861002025, 978-1861002020

More Books

Students also viewed these Databases questions

Question

What are the major advantages and disadvantages of a C corporation?

Answered: 1 week ago

Question

What is DDL?

Answered: 1 week ago