Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Question Write a Static method that accepts a List<T> argument. It should r

ID: 3759777 • Letter: J

Question

Java Question
Write a Static method that accepts a List<T> argument. It should return the number in the list that are equal to the element that comes before it.use only iterators to inspect the list. Do not use for loops or the get() method Java Question
Write a Static method that accepts a List<T> argument. It should return the number in the list that are equal to the element that comes before it.use only iterators to inspect the list. Do not use for loops or the get() method Java Question
Write a Static method that accepts a List<T> argument. It should return the number in the list that are equal to the element that comes before it.use only iterators to inspect the list. Do not use for loops or the get() method

Explanation / Answer

Solution:

package com.chegg.nancy.solution;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class Main {

   @SuppressWarnings({ "rawtypes", "unchecked" })
   public static <T> void main(String[] args) {

       List list = new ArrayList<T>();
       list.add(1);
       list.add(1);
       list.add(2);
       list.add(3);
       list.add(3);
       list.add(4);
       list.add(5);
       list.add(5);

       try {
           calStaticMethod(list);
       } catch (Exception e) {
           e.printStackTrace();
           System.out.println("Error!");
       } finally {
           System.out.println("Program Exited");
       }
   }

   @SuppressWarnings("rawtypes")
   private static void calStaticMethod(List list) {
       ListIterator itr = list.listIterator();
       while (itr.hasNext()) {

           int ele1 = (int) itr.next();
           int ele2 = (int) itr.next();
           if (ele1 == ele2)
               System.out.println(ele1 + " ," + ele2);
           if (itr.hasNext() == false)
               break;
           itr.previous();

       }

   }
}

Output:

1 ,1
3 ,3
5 ,5
Program Exited

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote