List in Java

ArrayList Class:


-> It contains duplicate elements
-> Maintains insertion order
-> Non synchronized
-> Allows random access because array works on basis of indexes
-> It extends AbstractList class and implements List
-> Manipulation is slow



Simple Example:
import java.util.ArrayList;
public class Test{
public static void main(String a[])
{
ArrayList<String> al=new ArrayList<String>();
al.add("Hello");
al.add("Java");
al.add("Learner");
System.out.println("ArrayList"+al);
}

}
Output:
>javac Test.java
>java Test

ArrayList[Hello, Java, Learner]

Key point:Default ArrayList capacity 10.Size of the ArrayList increased(How) by adding the item to it.
ArrayList Supports 3-constructors:
ArrayList(): creates empty ArrayList
ArrayList(Collection c): creates list and initializes with Collection c elements
ArrayList(int capacity): List created with the capacity mentioned and increased by adding the elements.

Methods inherited from its parent class are:
void add(int index, Object element)-Inserts the specified element at the specified position index in this list.
boolean add(Object o)-Appends the specified element to the end of this list
boolean addAll(Collection c)-Appends all of the elements in the specified collection to the end of this list
boolean addAll(int index, Collection c)-Inserts all of the elements in the specified collection into this list, starting at the specified position.
void 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.
Object get(int index)-Returns the element at the specified position in this list.
int 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.
int 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.
void clear()-Removes all of the elements from this list.
Object clone()-Returns a shallow copy of this ArrayList.
boolean contains(Object o)-Returns true if this list contains the specified element.
Object remove(int index)-Removes the element at the specified position in this list.
protected void removeRange(int fromIndex, int toIndex)-Removes from this List all of the elements whose index is between fromIndex(inclusive) and toIndex (exclusive)
Object set(int index, Object element)-Replaces the element at the specified position in this list with the specified element.
int size()-Returns the number of elements in this list.
Object[] toArray()-Returns an array containing all of the elements in this list in the correct order. Object[] 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.

void trimToSize()-Trims the capacity of this ArrayList instance to be the list's current size.



Example: 1
import java.util.ArrayList;
public class Test{
    public static void main(String[] a){
        ArrayList<Integer> al = new ArrayList<Integer>();
        al.add(1);
        al.add(2);
        al.add(1);
        al.add(3);
        System.out.println(al);
        System.out.println("Element at index 4: "+al.get(1));
        System.out.println("Does list contains 4"+al.contains(4));
        System.out.println("Is arraylist empty? "+al.isEmpty());
        System.out.println("Index of 2 is "+al.indexOf(2));
        System.out.println("Size List: "+al.size());
    }

}
Output:
>javac Test.java
>java Test
[1, 2, 1, 3]
Element at index 4: 2
Does list contains 4false
Is arraylist empty? false
Index of 2 is 1
Size List: 4

How the size of the ArrayList can be increased internally?
Before adding element into the array it(add method) will check what is the current size of filled elements and what is the maximum size of the array. If size of filled elements is greater than maximum size of the array then size of the array must be increased. New array is created with size of (3/2)*current arraysize and the old array copied to new array.

Why can't I make an ArrayList of integers?
ArrayLists hold objects, but primitive types are not considered objects, so Java has something called wrapper classes where you can make a primitive act as an object.
You can add integers by using wrapper classes of primitive data types.
For Example:

ArrayList<Integer> list=new ArrayList<Integer>();
list.add(1);
list.add(2);
.
.
.
list.add(n);
 







 

No comments:

Post a Comment