Java Vector subList() Method | Rookie Tutorial
\n\nRookie Tutorial -- Learning is not just technology, but also dreams!
\n\n- \n
- Home \n
- HTML \n
- JavaScript \n
- CSS \n
- Vue \n
- React \n
- Python3 \n
- Java \n
- C \n
- C++ \n
- C# \n
- AI \n
- Go \n
- SQL \n
- Linux \n
- VS Code \n
- Bootstrap \n
- Git \n
- Local Bookmarks \n
- \n
- Vue3 Tutorial \n
- Vue2 Tutorial \n
- \n
- Bootstrap3 \n
- Bootstrap4 \n
- Bootstrap5 \n
- \n
- Machine Learning\n
- PyTorch \n
- TensorFlow \n
- Sklearn \n
- NLP \n
- AI Agent \n
- Ollama \n
- Coding Plan \n
Java Tutorial
\n\nJava TutorialJava IntroductionJava Development Environment ConfigurationJava Basic SyntaxJava CommentsJava Objects and ClassesJava Basic Data TypesJava Variable TypesJava Variable Naming RulesJava ModifiersGeekTime>Java OperatorsJava Loop StructureJava Conditional StatementsJava switch caseJava Number & Math ClassJava Character ClassJava String ClassJava StringBufferJava ArraysJava Date TimeJava Regular ExpressionsJava MethodsGeekTime>Java Constructor MethodsJava Stream, File, IOJava Scanner ClassJava Exception Handling
\n\nJava Object-Oriented
\n\nJava InheritanceJava Override/OverloadJava PolymorphismJava Abstract ClassesJava EncapsulationJava InterfacesJava EnumsJava PackageJava Reflection
\n\nJava Advanced Tutorial
\n\nJava Data StructuresJava Collections FrameworkJava ArrayListJava LinkedListJava HashSetJava HashMapJava IteratorJava ObjectJava NIO FilesJava GenericsJava SerializationJava Network ProgrammingJava Sending EmailJava Multithreading ProgrammingJava Applet BasicsJava Documentation CommentsJava ExamplesJava 8 New FeaturesJava MySQL ConnectionJava 9 New FeaturesJava QuizJava Common Libraries
\n\n\n\n\n\nJava Vector subList() Method
\n\n\n\n\n\n
The Vector class is an important component of the Java Collections Framework, implementing dynamic array functionality. The subList() method is a utility method provided by the Vector class, used to extract a portion of elements from an existing Vector to form a new list view.
Method syntax:
\n\n\npublic List subList(int fromIndex, int toIndex)\n\n\n
Parameter Description
\n\n- \n
fromIndex: The starting index of the sublist (inclusive) \ntoIndex: The ending index of the sublist (exclusive) \n
Precautions
\n\n- \n
- Index values must satisfy
0 β€ fromIndex β€ toIndex β€ size()\n - If
fromIndexandtoIndexare equal, the returned sublist is empty \n - Negative parameters or out-of-range values will throw
IndexOutOfBoundsException\n
Return Value Characteristics
\n\nThe subList() method returns a view of the original Vector, not an independent copy. This means:
View Characteristics
\n\n- \n
- Modifications to the sublist will directly affect the original
Vector\n - Structural modifications to the original
Vector(such as adding/removing elements) will invalidate the sublist \n - The sublist supports all list operations \n
\n\n
Usage Example
\n\nExample
\n\n\nimport java.util.Vector;\nimport java.util.List;\n\npublic class VectorSubListExample {\n public static void main(String[] args) {\n // Create a Vector and add elements\n Vector colors = new Vector();\n colors.add("Red");\n colors.add("Green");\n colors.add("Blue");\n colors.add("Yellow");\n colors.add("Orange");\n \n System.out.println("Original Vector: " + colors);\n \n // Get sublist from index 1 (inclusive) to 4 (exclusive)\n List subList = colors.subList(1, 4);\n System.out.println("Sublist (1-4): " + subList);\n \n // Modify the sublist\n subList.set(0, "Purple");\n System.out.println("After modifying sublist: " + colors);\n \n // Clear the sublist\n subList.clear();\n System.out.println("After clearing sublist: " + colors);\n }\n}\n\n\nOutput:
\n\n\nOriginal Vector: [Red, Green, Blue, Yellow, Orange]\nSublist (1-4): [Green, Blue, Yellow]\nAfter modifying sublist: [Red, Purple, Blue, Yellow, Orange]\nAfter clearing sublist: [Red, Orange]\n
YouTip