Answered step by step
Verified Expert Solution
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 ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started