List.subList Method

Visual Studio 2005

Creates a sub list from the elements that exist within a specified index range in a List object.

Package: java.util

Assembly: vjslib (in vjslib.dll)

public abstract java.util.List subList(
    int fromIx,
    int toIx);



The starting index of the range.


The ending index of the range.

A list that contains the elements in the specified range.

// list-sublist1.jsl
// List.subList example

import java.util.*;

public class MyClass
    public static void main(String[] args)
        // Create a linked list object:
        LinkedList lList =  new LinkedList();

        // Add some elements:
        lList.add("My node");
        lList.add("Her node");
        lList.add("Their node");
        lList.add("Your node");

        // Display the list:
        System.out.println("The original list:");
        for (int i=0; i<lList.size(); i++)
            System.out.println(i + "=" + lList.get(i));

        // Create a sublist:
        List lList1 = lList.subList(2,4);

        // Display the sublist:
        System.out.println("\nThe sublist:");
        for (int i=0; i<lList1.size(); i++)
            System.out.println(i + "=" + lList1.get(i));

The original list:
0=My node
1=Her node
2=Their node
3=Your node

The sublist:
0=Their node
1=Your node

Throws IndexOutOfBoundsException if fromIx is negative, or toIx greater than size.

Throws IllegalArgumentException if toIx is less than fromIx.

Community Additions