Friday, 18 April 2014

Collections------------->>>>>>>>>>>List---------->>>>>ArrayList------------>>>>>>>>>>Array List Methods

Array List Methods:
SNMethods with Description
1void add(int index, Object element)
Inserts the specified element at the specified position index in this list. Throws IndexOutOfBoundsException if the specified index is is out of range (index < 0 || index > size()).
2boolean add(Object o) 
Appends the specified element to the end of this list.
3boolean addAll(Collection c)
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. Throws NullPointerException if the specified collection is null.
4boolean addAll(int index, Collection c) 
Inserts all of the elements in the specified collection into this list, starting at the specified position. Throws NullPointerException if the specified collection is null.
5void clear() 
Removes all of the elements from this list.
6Object clone() 
Returns a shallow copy of this ArrayList.
7boolean contains(Object o) 
Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).
8void ensureCapacity(int minCapacity) 
Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
9Object get(int index) 
Returns the element at the specified position in this list. Throws IndexOutOfBoundsException if the specified index is is out of range (index < 0 || index >= size()).
10int indexOf(Object o) 
Returns the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element.
11int lastIndexOf(Object o)
Returns the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element.
12Object remove(int index) 
Removes the element at the specified position in this list. Throws IndexOutOfBoundsException if index out of range (index < 0 || index >= size()).
13protected void removeRange(int fromIndex, int toIndex) 
Removes from this List all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive.
14Object set(int index, Object element) 
Replaces the element at the specified position in this list with the specified element. Throws IndexOutOfBoundsException if the specified index is is out of range (index < 0 || index >= size()).
15int size() 
Returns the number of elements in this list.
16Object[] toArray() 
Returns an array containing all of the elements in this list in the correct order. Throws NullPointerException if the specified array is null.
17Object[] toArray(Object[] a) 
Returns an array containing all of the elements in this list in the correct order; the runtime type of the returned array is that of the specified array.
18void trimToSize() 
Trims the capacity of this ArrayList instance to be the list's current size.


import java.util.*;
public class ArrayListDemo1

{
public static void main(String[] args)
{
ArrayList al=new ArrayList();
al.add("hi");
al.add("you");
System.out.println(al);

System.out.println(al.contains("hi"));
System.out.println(al.size());
;


}
}
//adding  objects  to AL.

import java.util.*;
public class Ald
{
public static void main(String[] args)
{

ArrayList al=new ArrayList();
al.add(10);
al.add(20);
al.add(30);
al.add(40);
al.add(50);
al.add(60);
al.add(70);

System.out.println(al);
al.set(0,"anvesh");
System.out.println(al);
al.remove(1);
System.out.println(al);

ArrayList bl=new ArrayList();
bl.add(1);
bl.add(2);
bl.add(3);
bl.add(4);
bl.add(5);
bl.add(6);
bl.add(7);
bl.addAll(al);
//bl.retainAll(collection("20","30"))
System.out.println(bl);
bl.clear();
System.out.println(al);

}
}

http://www.tutorialspoint.com/java/java_arraylist_class.htm

No comments:

Post a Comment