Question: 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

1 Expert Approved Answer
Step: 1 Unlock

The correct implementation to insert an element into an ArrayList before every even index is public ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!