Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Which of these method implementations would correctly insert an element into an ArrayList before every even index? public void addEven(ArrayList array, E element) { for(int


Which of these method implementations would correctly insert an element into an ArrayList before every even index?


public void addEven(ArrayList array, E element) { for(int index = 1; index < array.size(); index++) { if(index %2 == 0) { array.add(index+ 1, element); index--; } } }
public void addEven(ArrayList array, E element) { for(int index = 0; index < array.size(); index++) { if(index %2 == 0) { array.add(index, element); index++; } } }
public void addEven(ArrayList array, E element) { for(int index = 0; index < array.size(); index++) { if(index %2 == 0) { array.add(index -1, element); index++; } } }
public void addEven(ArrayList array, E element) { for(int index = 0; index < array.size(); index++) { if(index %2 == 0) { array.add(index, element); index++; index++; } } }




2. Which of these methods will properly traverse two ArrayLists and print any index that have the same value in both ArrayLists?


public void printSharedValues(ArrayList array1, ArrayList array2) { int size; if(array1.size() > array2.size()) { size = array2.size(); } else { size = array1.size(); } while(index < size) { int index = 0; if(array1.get(index) == array2.get(index)) { System.out.println(index); } index++; } }
public void printSharedValues(ArrayList array1, ArrayList array2) { int index = 0; while(index < array1.size()) { if(array1.get(index) == array2.get(index)) { System.out.println(index); } index--; } }
public void printSharedValues(ArrayList array1, ArrayList array2) { int index = 0; int size; if(array1.size() > array2.size()) { size = array2.size(); } else { size = array1.size(); } while(index < size) { if(array1.get(index) == array2.get(index)) { System.out.println(index); } index++; } }
public void printSharedValues(ArrayList array1, ArrayList array2) { int index = 0; while(index < array1.size()) { if(array1.get(index) == array2.get(index)) { System.out.println(index); } index++; } }

Step by Step Solution

3.44 Rating (160 Votes )

There are 3 Steps involved in it

Step: 1

The correct implementation to insert an element into an ArrayList before every even index is public ... 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

Introduction to Java Programming, Comprehensive Version

Authors: Y. Daniel Liang

10th Edition

133761312, 978-0133761313

More Books

Students also viewed these Programming questions

Question

=+7.2 Large-Sample Confidence Intervals for a Population Mean

Answered: 1 week ago