Created
December 24, 2021 08:35
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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