Skip to content

Instantly share code, notes, and snippets.

@gauravkukade
Created December 24, 2021 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gauravkukade/f411ebb758b6cc5b9db47d317fb02eaf to your computer and use it in GitHub Desktop.
Save gauravkukade/f411ebb758b6cc5b9db47d317fb02eaf to your computer and use it in GitHub Desktop.
A java program to remove the range of elements from the MyArraylist (a class which extends the ArrayList) using the removeRange() method. The removeRange() method is protected and is accessed in class, subclasses and in a package, but not public. Read more at https://coderolls.com/arraylist-removerange-method/
package com.gaurav.ExProject.ArrayList;
import java.util.ArrayList;
import java.util.List;
/**
* A java program to remove the range of elements from
* the MyArraylist (a class which extends the ArrayList)
* using the removeRange() method.
*
* The removeRange() method is protected and is accessed in class,
* subclasses and in a package, but not public.
*
* @author Gaurav Kukade at coderolls.com
*
*/
public class MyArrayList extends ArrayList<String> {
public static void main(String[] args) {
MyArrayList myArrayList = new MyArrayList();
myArrayList.add("Monday");
myArrayList.add("Tuesday");
myArrayList.add("Wednesday");
myArrayList.add("Thursday");
myArrayList.add("Friday");
myArrayList.add("Saturday");
myArrayList.add("Sunday");
System.out.println("Arraylist before removing range of elments:\n"+ myArrayList);
myArrayList.removeRange(2, 4);
System.out.println("\nArraylist after removing range of elments:\n"+ myArrayList);
}
}
Arraylist before removing range of elments:
[Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]
Arraylist after removing range of elments:
[Monday, Tuesday, Friday, Saturday, Sunday]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment