Fill This Form To Receive Instant Help

Help in Homework
trustpilot ratings
google ratings


Homework answers / question archive / 1) Which of these method implementations would correctly insert an element into an ArrayList before every even index?   public void addEven(ArrayList<E> array, E element) { for(int index = 1; index < array

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

Computer Science

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

 

public void addEven(ArrayList<E> 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<E> array, E element)
{
   for(int index = 0; index < array.size(); index++)
   {
      if(index %2 == 0)
      {
         array.add(index, element);
         index++;
      }
   }
}
public void addEven(ArrayList<E> 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<E> 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<Integer> array1, ArrayList<Integer> 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<Integer> array1, ArrayList<Integer> array2)
{
   int index = 0;
   while(index < array1.size())
   {
      if(array1.get(index) == array2.get(index))
      {
         System.out.println(index);
      }
      index--;
   }
}
public void printSharedValues(ArrayList<Integer> array1, ArrayList<Integer> 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<Integer> array1, ArrayList<Integer> array2)
{
   int index = 0;
   while(index < array1.size())
   {
      if(array1.get(index) == array2.get(index))
      {
         System.out.println(index);
      }
      index++;
   }
}

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE