List

An interface from the collection classes.

Any non-trivial program needs a dynamically growing collection for storing objects created at runtime.

The java.util package provides several classes to suit different programming requirements.

The Iterator provides the methods to loop through the List, like a for loop. The difference here is that the underlying data structure is not accessed directly.

ArrayList

It extends AbstractList and implements Collection, List and Iterable interfaces.

The ArrayList is an implementation of a dynamically growing array.

The ArrayList stores its elements in insertion order.

Like an array, the index is zero-based, and its size re-adjusts whenever elements are added or removed.

It stores only objects, so primitives will be auto-boxed for storing.

It is possible to add any kind of object into an array, but while reading back the elements, they need to be cast to the correct object, otherwise a ClassCastException will be thrown.

ArrayList list = new ArrayList();
list.add("Xander Cage");
list.add("Toretto");
int size = list.size();
for(int i=0; i<size; i++) {
	String str = (String)list.get(i);	// cast required, from Object to String
}

Parameterised Types a.k.a Generics

To prevent class cast exceptions, Java allows collection classes to store only parameterised types of objects. It also provides compile-time type checking.

More on Generics in a later post.

An example

// Product.java
package ecom;

public class Product
{
	private String name;
	private int quantity;
	private double price;

	public Product(String name, int quantity, double price)
	{
		this.name = name;
		this.quantity = quantity;
		this.price = price;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getName()
	{
		return name;
	}

	public void setQuantity(int quantity)
	{
		this.quantity = quantity;
	}

	public int getQuantity()
	{
		return quantity;
	}

	public void setPrice(double price)
	{
		this.price = price;
	}

	public double getPrice()
	{
		return price;
	}

	@Override
	public String toString()
	{
		return name+":"+quantity+":"+price;
	}
}

// Catalogue.java
package ecom;
import java.util.*;

public class Catalogue
{
	private ArrayList<Product> productList;
	
	public Catalogue() {
		productList = new ArrayList<Product>();
	}
	
	public void add(Product product) {
		productList.add(product);
	}
	
	public void showProducts() {
		Iterator<Product> iter = productList.iterator();
		while(iter.hasNext()) {
			// no cast necessary
			Product P = iter.next();
			System.out.println(P);
		}
	}
	
	public static void main(String[] args) {
		Product shoes = new Product("Adidas",200,1200);
		Product skirts = new Product("Neeru's",50,300);
		Catalogue shelf = new Catalogue();
		shelf.add(shoes);
		shelf.add(skirts);
		shelf.showProducts();
	}
}

LinkedList
Implemented as a double-linked list, LinkedList doesn’t allow insertion​ and removal of elements at arbitrary positions.

Traversal can begin from any node and navigate towards the beginning or the end of the list.

It is used when applications perform frequent insertions and deletions from a list, and where contiguous memory locations are not a requirement.

Advertisements

Author: Anand Betanabhotla

Corporate Trainer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s