Hackerss.com

hackerss
hackerss

Posted on

Remove the first two elements of a list in Java

Remove the first two elements of a list - Java:


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

public class RemoveFirstTwoElements {
   /**
   * This method remove the first two elements of a list.
   * @param list the list to remove the first two elements.
   * @return List Obtains the list without the first two elements.
   */   
   public List<String> removeFirstTwoElements(List<String> list) {
       //Remove the first two elements
       list.remove(0);
       list.remove(0);
       //Return the list without the first two elements
       return list;
   }

   /**
   * One Example
   */
   public static void main(String args[]) throws IOException {

      RemoveFirstTwoElements  removeFirstTwoElements= new RemoveFirstTwoElements ();
      //Example
      List<String> list = new ArrayList<String>();
      list.add("A");
      list.add("B");
      list.add("C");
      list.add("D");
      list = removeFirstTwoElements.removeFirstTwoElements(list);
      //Output
      System.out.println("List without the first two elements is :" + list); //[C, D]

   }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)