Core Java Tutorials


Introduction

Java Hello World

Java Compilation

JDK New Features

JVM Architecture

Java OOPs Concepts

Java Package

Array

Exception Handling

String

Immutable Class

File Handling

Serialization

MultiThreading

Volatile

Concurrent Package

Collection

Collection Internal

Generics

Cloning

Reflection In Java

Annotation

Class Loader

Java Inner classes

Garbage Collector

JDBC

References In Java

Heap Dump & Thread Dump

ArrayList in Collection class Hierarchy::


ArrayList Example with String:-

import java.util.*;
public class ArrayListDemo {
	public static void main(String[] args) {
	// Generic Declaration
	// collection classes can be used as container for other objects.
	List<String> list = new ArrayList<String>(); //10
	list.add("Apple");
	list.add("Mango");
	list.add("Orange");
	list.add("Banana");
	list.add("Papaya");
	list.add("Papaya");

	//new capacity = (old capacity*3)/2 +1 = 16
		
	Iterator it = list.iterator();
	//Iterator can be used to access any collection which 
	//implements Iterable interface
	while (it.hasNext()) {
		String element = it.next();
		System.out.println(element);
	}

	// retrieving all the elements.jdk 1.5
	for (String str : list) {
		System.out.println(str);
	}
	// retrieving all the elements.
	// fastest retrieval because access as per index of the array.
	//a[0]
	for (int i = 0; i < list.size(); i++) {
		System.out.println("list.get(i));//A[i]
	}

	ListIterator listIterator = list.listIterator();
	while (listIterator.hasNext()) {
		System.out.println("listIterator.next());
		listIterator.add("Nagendra");
	}

	//searching an element Apple inside the ArrayList
	boolean b=list.contains("Apple"); //complexity  = O(n)		
  }
}

ArrayList Example with Biscuit class:-

Defining a class Biscuit

Array List code with Biscuit

Searching a Biscuit in ArrayList Searching complexity of an element into ArrayList is O(n).So ArrayList is not good for searching.

Sorting Biscuit in ArrayList