Cill
Cill
Arrays are used to represent group of elements as a single entity but these
elements are homogeneous & fixed size.
The size of Array is fixed it means once we created Array it is not possible to
increase and decrease the size.
Array in java is index based first element of the array stored at 0 index.
Advantages of array:-
Instead of declaring individual variables we can declare group of elements
by using array it reduces length of the code.
We can store the group of objects easily & we are able to retrieve the data
easily.
We can access the random elements present in the any location based on
index.
declaration & instantiation & initialization :-
Approach 1:- int a[]={10,20,30,40}; //declaring, instantiation, intialization
Approach 2:- int[] a=new int[100]; //declaring, instantiation
a[0]=10; //initialization
a[1]=20;
;;;;;;;;;;;;;;;;;;;;;;
a[99]=40;
// declares an array of integers
int[] anArray;
// allocates memory for 10 integers
anArray = new int[10];
// initialize first element
anArray[0] = 10;
// initialize second element
anArray[1] = 20;
// and so forth
anArray[2] = 30; anArray[3] = 40; anArray[4] = 50; anArray[5] = 60;
anArray[6] = 70; anArray[7] = 80; anArray[8] = 90; anArray[9] = 100;
Collection(Interface)
Collection vs Collections:-
Collection is interface it is used to represent group of objects as a single entity.
Collections is utility class it contains methods to perform operations.
Note: - The root interface of Collection framework is Collection it contains 15 methods so all
Implementation classes are able to use that methods.
public abstract int size();
public abstract boolean isEmpty();
public abstract boolean contains(java.lang.Object);
public abstract java/util/Iterator<E> iterator();
public abstract java.lang.Object[] toArray();
public abstract <T extends java/lang/Object> T[] toArray(T[]);
public abstract boolean add(E);
public abstract boolean remove(java.lang.Object);
public abstract boolean containsAll(java/util/Collection<?>);
public abstract boolean addAll(java/util/Collection<? extends E>);
public abstract boolean removeAll(java/util/Collection<?>);
public abstract boolean retainAll(java/util/Collection<?>);
public abstract void clear();
public abstract boolean equals(java.lang.Object);
public abstract int hashCode();
Java.util.ArrayList:-
ArrayList is implementing List interface it widely used class in projects because it is
providing functionality and flexibility.
ArrayList Characteristics:-
ArrayList Introduced in 1.2 version.
ArrayList stores Heterogeneous objects (different types).
Inside ArrayList we can insert Null objects.
ArrayList preserved Insertion order it means whatever the order we
inserted the data in the same way output is printed.
ArrayList methods are non-synchronized methods.
Duplicate objects are allowed.
The under laying data structure is growable array.
Constructors
ArrayList al = new ArrayList(); (Default capacity - 10).
ArrayList al = new ArrayList ( int initial-capacity)
ArrayList al = new ArrayList(Collection c);
Example :-Collections Autoboxing
upto 1.4 version by using wrapper classes, create objects then add that objects in ArrayList.
import java.util.ArrayList;
class Test
{ public static void main(String[] args)
{ ArrayList al = new ArrayList();
Integer i = new Integer(10); //creation of Integer Object
Character ch = new Character('c'); //creation of Character Object
Double d = new Double(10.5); //creation of Double Object
//adding wrapper objects into ArrayList
al.add(i);
al.add(ch);
al.add(d);
System.out.println(al);
}
But from 1.5 version onwards autoboxing concept is introduced so add the primitive value
directly that is
automatically converted into wrapper objects format.
import java.util.ArrayList;
class Test
{ public static void main(String[] args)
{ ArrayList al = new ArrayList();
al.add(10); //primitive int value --->Integer Object conversion //AutoBoxing
al.add('a'); //primitive char value --->Integer Object conversion //AutoBoxing
al.add(10.5); //primitive double value --->Integer Object conversion //AutoBoxing
System.out.println(al);
}
}
Methods of ArrayList
Method Description
void add(int index, E element) It is used to insert the specified element at the specified p
boolean addAll(Collection<? extends It is used to append all of the elements in the specified col
E> c) this list, in the order that they are returned by the specifie
iterator.
boolean addAll(int index, Collection<? It is used to append all the elements in the specified collec
extends E> c) specified position of the list.
void clear() It is used to remove all of the elements from this list.
E get(int index) It is used to fetch the element from the particular position
Java new generic collection allows you to have only one type of object in a collection. Now it
is type safe so typecasting is not required at runtime.
1. import java.util.*;
public class ArrayListExample2{
public static void main(String args[]){
ArrayList<String> list=new ArrayList<String>();//Creating arraylist
list.add("C");//Adding object in arraylist
list.add("Java");
list.add("Python");
list.add("Spring");
//Traversing list through Iterator
Iterator itr=list.iterator();//getting the Iterator
while(itr.hasNext()){//check if iterator has the elements
System.out.println(itr.next());//printing the element and move to next
}
}
}
Sort ArrayList
Collections.sort(sourceList)
LinkedList()
LinkedList(Collection<? extends E> c)
Methods similar to Array list an additional methods of linked list will be present.
ArrayList LinkedList
3) An ArrayList class can act as a LinkedList class can act as a list and
list only because it implements List only. queue both because it implements List
and Deque interfaces.
Example:-
import java.util.*;
class Test
{ public static void main(String[] args)
{ Stack<String> s = new Stack<String>();
s.push("ratan"); //insert the data top of the stack
s.push("anu"); //insert the data top of the stack
s.push("Sravya");
System.out.println(s);
System.out.println(s.size());
System.out.println(s.peek()); //to return last element of the Stack
s.pop(); //remove the data top of the stack
System.out.println(s);
System.out.println(s.isEmpty());
s.clear();
System.out.println(s.isEmpty());
}
}
HashSet:-
1) Introduced in 1.2 v.
2) Duplicate objects are not allowed if we are trying to insert duplicate values then we won't get
any compilation errors an won't get any Execution errors simply add method return old value.
3) Null insertion is possible but if we are inserting more than one null it return only one null
value.
4) Heterogeneous objects are allowed.
5) The under laying data structure is HashTable.
6) It is not maintain any order the elements are return in any random order .[Insertion order is
not
preserved].
7) Methods are non-synchronized.
8) cursor : Iterator.