Thursday 8 May 2014

difference between iterator,Arraylist and list iterator in java


Arraylist :

-> ArrayList is an actual data structure, an implementation of the List interface
-> ArratList is actual list of the object content the value.
-> Its have the method like add(object), remove(object), get(index), etc.


1) boolean add(E e) - This method appends the specified element to the end of this list.

2) void add(int index, E element) - This method inserts the specified element at the specified position in this list.

3) boolean addAll(Collection<? extends E> c) - This method appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator

4) boolean addAll(int index, Collection<? extends E> c) - This method inserts all of the elements in the specified collection into this list, starting at the specified position.

5) void clear() - This method removes all of the elements from this list.

6) Object clone() - This method returns a shallow copy of this ArrayList instance.

7) boolean contains(Object o) - This method returns true if this list contains the specified element.

8) void ensureCapacity(int minCapacity) - This increases the capacity of this ArrayList.

9) E get(int index) - This method returns the element at the specified position in this list.

10) int indexOf(Object o) - This method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

11) boolean isEmpty() - This method returns true if this list contains no elements.

12) int lastIndexOf(Object o) - This method returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.

13) E remove(int index) - This method removes the element at the specified position in this list.

14) boolean remove(Object o) - This method removes the first occurrence of the specified element from this list, if it is present.

15) protected void removeRange(int fromIndex, int toIndex) - This method removes from this list all of the elements whose index is between fromIndex(inclusive) and toIndex(exclusive).

16) E set(int index, E element) - This method replaces the element at the specified position in this list with the specified element.

17) int size() - This method returns the number of elements in this list.

18) Object[] toArray() - This method returns an array containing all of the elements in this list in proper sequence (from first to last element).

19) <T> T[] toArray(T[] a) - This method returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that
of the specified array.

20) void trimToSize() - This method trims the capacity of this ArrayList instance to be the list's current size.




Iterator :

-> Iterator is just an interface that allows you to navigate through any data structure.
-> Iterator is helpful for navigate and access the data.
-> Iterator have the below method
-> Iterator is used for map, set, and list.

1) boolean hasNext( ) - Returns true if there are more elements. Otherwise, returns false.

2) Object next( ) - 1Returns the next element. Throws NoSuchElementException if there is not a next element.

3) void remove( ) - Removes the current element. Throws IllegalStateException if an attempt is made to call remove( ) that is not preceded by a call to next( ).



ListIterator:

-> ListIterator is a subclass which extends Iterator.
-> ListIterator allow bidirectional traversal of a list, and the modification of elements.
-> ListIterator is only used for lists, and not used for map, set, and list.


1) void add(Object obj) - Inserts obj into the list in front of the element that will be returned by the next call to next( ).

2) boolean hasNext( ) - Returns true if there is a next element. Otherwise, returns false.

3) boolean hasPrevious( ) - Returns true if there is a previous element. Otherwise, returns false.

4) Object next( ) - Returns the next element. A NoSuchElementException is thrown if there is not a next element.

5) int nextIndex( ) - Returns the index of the next element. If there is not a next element, returns the size of the list.

6) Object previous( ) - Returns the previous element. A NoSuchElementException is thrown if there is not a previous element.

7) int previousIndex( ) - Returns the index of the previous element. If there is not a previous element, returns -1.

8) void remove( ) - Removes the current element from the list. An IllegalStateException is thrown if remove( ) is called before next( ) or previous( ) is invoked.

9) void set(Object obj) - Assigns obj to the current element. This is the element last returned by a call to either next( ) or previous( ).

No comments:

Post a Comment