Skip to Content

Remove specified element from LinkedList Java example

  • strict warning: Non-static method view::load() should not be called statically in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/views.module on line 906.
  • strict warning: Declaration of views_handler_argument::init() should be compatible with views_handler::init(&$view, $options) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_argument.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter.inc on line 0.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/handlers/views_handler_filter_boolean_operator.inc on line 0.
  • strict warning: Declaration of views_plugin_style_default::options() should be compatible with views_object::options() in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_style_default.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/javaexa1/public_html/sites/all/modules/views-6.x-2.16/views/plugins/views_plugin_row.inc on line 0.

  1. /*
  2.   Remove element from LinkedList Java example
  3.   This java example shows how to remove a particular element from Java LinkedList.
  4.   It also shows how to remove an element at specified index from LinkedList.
  5. */
  6.  
  7. import java.util.LinkedList;
  8.  
  9. public class RemoveElementLinkedListExample {
  10.  
  11.   public static void main(String[] args) {
  12.  
  13.     //create LinkedList object
  14.     LinkedList lList = new LinkedList();
  15.  
  16.     //add elements to LinkedList
  17.     lList.add("1");
  18.     lList.add("2");
  19.     lList.add("3");
  20.     lList.add("4");
  21.     lList.add("5");
  22.  
  23.     System.out.println("LinkedList contains : " + lList);
  24.    
  25.     /*
  26.      * To remove a specified element from Java LinkedList, use
  27.      * boolean remove(Object obj) method.
  28.      *
  29.      * This method removes the first occurrence of the specified element from
  30.      * Java LinkedList and returns true if LinkedList contained the specified
  31.      * element.
  32.      *
  33.      * If LinkedList did not contain the specified element, it remains unchanged.
  34.      */
  35.    
  36.      boolean isRemoved = lList.remove("2");
  37.      System.out.println("Is 2 removed from LinkedList ? :" + isRemoved);
  38.      System.out.println("LinkedList now contains : " + lList);
  39.    
  40.      /*
  41.       * To remove an element at specified index of LinkedList, use
  42.       * Object remove(int index) method.
  43.       *
  44.       * This method removes an element from specified index and shifts subsequent
  45.       * elements to the left. It returns an element previously at the specified
  46.       * index.
  47.       */
  48.      
  49.       Object obj = lList.remove(2);
  50.       System.out.println(obj + " has been removed from LinkedList");
  51.       System.out.println("LinkedList now contains : " + lList);
  52.   }
  53. }
  54.  
  55. /*
  56. Output would be
  57.  
  58. LinkedList contains : [1, 2, 3, 4, 5]
  59. Is 2 removed from LinkedList ? :true
  60. LinkedList now contains : [1, 3, 4, 5]
  61. 4 has been removed from LinkedList
  62. LinkedList now contains : [1, 3, 5]
  63. */