Skip to content

Instantly share code, notes, and snippets.

@gauravkukade
Created December 24, 2021 08:35
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/1681c8aaf558eaf12f6046b7d36bbc91 to your computer and use it in GitHub Desktop.
Save gauravkukade/1681c8aaf558eaf12f6046b7d36bbc91 to your computer and use it in GitHub Desktop.
A java program to explain the subList() method of the ArrayList class in Java. Read more at https://coderolls.com/sublist-method-in-arraylist/
import java.util.ArrayList;
import java.util.List;
/**
* A java program to explain the subList() method of the ArrayList class in Java
*
* @author Gaurav Kukade at coderolls.com
*
*/
public class ArrayListSubListExample {
public static void main(String[] args) {
// create an empty arraylist object 'states'
List<String> states = new ArrayList<String>();
// add state in the arraylist
states.add("California");
states.add("Texas");
states.add("Florida");
states.add("New Jersey");
states.add("Washington");
System.out.println("The states list: \n"+ states);
// sublist the states list from 1 to 3 index
List<String> statesSubList = states.subList(1, 3);
System.out.println("\nThe states sublist from index 1 (inclusive) to 3 (exclusive): \n"+ statesSubList);
}
}
The states list:
[California, Texas, Florida, New Jersey, Washington]
The states sublist from index 1 (inclusive) to 3 (exclusive):
[Texas, Florida]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment