Hackerss.com

Hackerss.com is a community of amazing hackers

Hackerss is a community for developers, data scientitst, ethical hackers, hardware enthusiasts or any person that want to learn / share their knowledge of any aspect of digital technology.

Create account Log in
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

Oldest comments (0)