The subList() method of AbstractList class in Java

Java 8Object Oriented ProgrammingProgramming

<p>The subList() method returns a part of this list between the specified fromIndex, inclusive, and toIndex, exclusive. Get a sublist using the method by setting the range as the two parameters.</p><p>The syntax is as follows.</p><pre class="result notranslate">public List&lt;E&gt; subList(int fromIndex, int toIndex)</pre><p>Here, the parameter fromIndex is the low endpoint (inclusive) of the subList and toIndex is the high endpoint (exclusive) of the subList.</p><p>To work with the AbstractList class, import the following package.</p><pre class="result notranslate">import java.util.AbstractList;</pre><p>The following is an example to implement subList() method of the AbstractlList class in Java.</p><h2>Example</h2><pre class="prettyprint notranslate">import java.util.ArrayList; import java.util.AbstractList; public class Demo { &nbsp; &nbsp;public static void main(String[] args) { &nbsp; &nbsp; &nbsp; AbstractList&lt;Integer&gt; myList = new ArrayList&lt;Integer&gt;(); &nbsp; &nbsp; &nbsp; myList.add(5); &nbsp; &nbsp; &nbsp; myList.add(20); &nbsp; &nbsp; &nbsp; myList.add(35); &nbsp; &nbsp; &nbsp; myList.add(47); &nbsp; &nbsp; &nbsp; myList.add(55); &nbsp; &nbsp; &nbsp; myList.add(70); &nbsp; &nbsp; &nbsp; myList.add(100); &nbsp; &nbsp; &nbsp; myList.add(140); &nbsp; &nbsp; &nbsp; myList.add(180); &nbsp; &nbsp; &nbsp; myList.add(250); &nbsp; &nbsp; &nbsp; System.out.println(&quot;Elements in AbstractList = &quot; + myList); &nbsp; &nbsp; &nbsp; List&lt;Integer&gt; list = myList.subList(5, 7); &nbsp; &nbsp; &nbsp; System.out.println(&quot;Sublist of AbstractList: &quot;+ list); &nbsp; &nbsp;} }</pre><h2>Output</h2><pre class="result notranslate">Elements in AbstractList = [5, 20, 35, 47, 55, 70, 100, 140, 180, 250] Sublist of AbstractList: [70, 100]</pre>
raja
Updated on 30-Jul-2019 22:30:25

Advertisements