AbstractList class is an abstract class which extends the class AbstractCollection and implements the interface List. In an unmodifiable list the programmers just required to extend the class AbstractList class and allows the implementation for the methods get(int) method and size() method i.e. these methods will be implemented into their appropriate class.
Java Collection Framework - AbstractList
AbstractList class is an abstract class which extends the class AbstractCollection and implements the interface List. In an unmodifiable list the programmers just required to extend the class AbstractList class and allows the implementation for the methods get(int) method and size() method i.e. these methods will be implemented into their appropriate class. But in a modifiable list there will must be required to override the extra methods additionally. AbstractList is a part of Java collection framework and is available in java.util package.
Syntax
public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E>
Constructor of AbstractList
protected AbstractList() : The only constructor of this class which is implicitly invoked by the subclass constructor.
Methods of AbstractList
This class provides various methods some of them are :
- add()
- clear()
- iterator()
- subList
- remove()
- get()
syntax : public boolean add(E e)
syntax : public void clear()
syntax : public Iterator<E> iterator()
syntax : public List<E> subList(int fromIndex, int toIndex)
syntax : public E remove(int index)
syntax : public abstract E get(int index)
Example
package devmanuals.com; import java.util.AbstractList; import java.util.List; import java.util.ArrayList; public class AbstractListDemo extends AbstractList { List list; AbstractListDemo(){ list = new ArrayList(); } public Object get(int i) { System.out.println(list.get(i)); return null; } public int size() { System.out.println(list.size()); return 0; } public boolean add(Object e) { list.add(e); System.out.println(list); return false; } public static void main(String... abc){ AbstractListDemo ald= new AbstractListDemo(); System.out.println("Elements of List ="); for(int i=0; i<=5; i++){ ald.add(i); } System.out.println("Size of list ="); ald.size(); System.out.println("Element at position 2"); ald.get(2); } }
Output :
Elements of List = [0] [0, 1] [0, 1, 2] [0, 1, 2, 3] [0, 1, 2, 3, 4] [0, 1, 2, 3, 4, 5] Size of list = 6 Element at position 2 2 |
[ 0 ] Comments