YouTip LogoYouTip

Java Vector Sublist

Java Vector subList() Method | Rookie Tutorial\n\n

Java Vector subList() Method | Rookie Tutorial

\n\n

Rookie Tutorial -- Learning is not just technology, but also dreams!

\n\n\n\n\n\n\n\n\n\n\n\n

Java Tutorial

\n\n

Java 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\n

Java Object-Oriented

\n\n

Java InheritanceJava Override/OverloadJava PolymorphismJava Abstract ClassesJava EncapsulationJava InterfacesJava EnumsJava PackageJava Reflection

\n\n

Java Advanced Tutorial

\n\n

Java 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

Java Vector size() Method

\n\n

Java Vector toArray() Method

\n\n

Java Vector subList() Method

\n\n

Image 3: Java Vector Java Vector

\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.

\n\n

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)
  • \n
  • toIndex: The ending index of the sublist (exclusive)
  • \n
\n\n

Precautions

\n\n
    \n
  1. Index values must satisfy 0 ≀ fromIndex ≀ toIndex ≀ size()
  2. \n
  3. If fromIndex and toIndex are equal, the returned sublist is empty
  4. \n
  5. Negative parameters or out-of-range values will throw IndexOutOfBoundsException
  6. \n
\n\n

Return Value Characteristics

\n\n

The subList() method returns a view of the original Vector, not an independent copy. This means:

\n\n

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
\n\n

Usage Example

\n\n

Example

\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\n

Output:

\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
← Java Vector TostringJava Vector Setsize β†’