Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Collections - Web Design and Development - Lecture Handouts, Lecture notes of Web Design and Development

Collections, Collections Design, Collection messages, Array List, Useful Methods, Using ArrayList class, Output, HashMap, Useful Methods, Basic messages, Constructor. Virtual University is one of best in Pakistan for distance education in science.

Typology: Lecture notes

2011/2012

Uploaded on 11/10/2012

taariq
taariq 🇵🇰

4.4

(16)

61 documents

1 / 7

Toggle sidebar

Related documents


Partial preview of the text

Download Collections - Web Design and Development - Lecture Handouts and more Lecture notes Web Design and Development in PDF only on Docsity!

Web Design & Development CS-

Collections

A collection represents group of objects know as its elements. Java has a built-in support

for collections. Collection classes are similar to STL in C++. An advantage of a

collection over an array is that you don’t need to know the eventual size of the collection

in order to add objects to it. The java.util package provides a set of collection classes

that helps a programmer in number of ways.

Collections Design

All classes almost provides same methods like get(), size(), isEmpty() etc. These methods

will return the object stored in it, number of objects stored and whether collection

contains an object or not respectively.

Java collections are capable of storing any kind of objects. Collections store references to

objects. This is similar to using a void* in C. therefore down casting is required to get the

actual type. For example, if string in stored in a collection then to get it back, we write

String element = (String)arraylist.get(i);

Collection messages

Some basic messages (methods) are:

ƒ Constructor

— creates a collection with no elements

ƒ int size()

— returns the number of elements in a collection

ƒ boolean add(Object)

— adds a new element in the collection

— returns true if the element is added successfully false otherwise

ƒ boolean isEmpty()

— returns true if this collection contains no element false otherwise

ƒ boolean contains(Object)

— returns true if this collection contains the specified element by using iterative

search

ƒ boolean remove(Object)

— removes a single instance of the specified element from this collection, if it is

present

Web Design & Development CS-

ArrayList

It’s like a resizable array. ArrayList actually comes as a replacement the old “Vector”

collection. As we add or remove elements into or from it, it grows or shrinks over time.

Useful Methods

ƒ add (Object)

— With the help of this method, any object can be added into ArrayList because

Object is the superclass of all classes.

— Objects going to add will implicitly upcast.

ƒ Object get(int index)

— Returns the element at the specified position in the list

— index ranges from 0 to size()-

— must cast to appropriate type

ƒ remove (int index)

— Removes the element at the specified position in this list.

— Shifts any subsequent elements to the left (subtracts one from their indices).

ƒ int size( )

Web Design & Development CS-

Example Code: Using ArrayList class

We’ll store Student objects in the ArrayList. We are using the same student class which

we built in previous lectures/handouts.

We’ll add three student objects and later prints all the student objects after retrieving

them from ArrayList. Let’s look at the code

iport java.util.*;

public class ArrayListTest {

public static void main(String[] args) {

// creating arrayList object by calling constructor ArrayList al= new ArrayList();

// creating three Student objects Student s1 = new Student (“ali” , 1); Student s2 = new Student (“saad” , 2); Student s3 = new Student (“raza” , 3);

// adding elements (Student objects) into arralylist al.add(s1); al.add(s2); al.add(s3);

// checking whether arraylist is empty or not boolean b = al.isEmpty ();

if (b = = true) { System.out.println(“arraylist is empty”);

} else { int size = al.size(); System.out.println(“arraylist size: ” + size); }

// using loop to iterate. Loops starts from 0 to one // less than size for (int i=0; i<al.size(); i++ ){

// retrieving object from arraylist Student s = (Student) al.get(i);

// calling student class print method s.print();

} // end for loop

Web Design & Development CS-

} // end main } // end class

Output

Web Design & Development CS-

HashMap

Store elements in the form of key- value pair form. A key is associated with each object

that is stored. This allows fast retrieval of that object. Keys are unique.

Useful Methods

ƒ put(Object key, Object Value)

— Keys & Values are stored in the form of objects (implicit upcasting is

performed).

— Associates the specified value with the specified key in this map.

— If the map previously contained a mapping for this key, the old value is

replaced.

ƒ Object get(Object key)

— Returns the value to which the specified key is mapped in this identity hash

map, or null if the map contains no mapping for this key.

— Must downcast to appropriate type when used

ƒ int size( )

Web Design & Development CS-

Example Code: using HashMap class

In this example code, we’ll store Student objects as values and their rollnos in the form of

strings as keys. Same Student class is used. The code is;

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

// creating HashMap object HashMap h= new HashMap();

// creating Student objects Student s1 = new Student (“ali” , 1); Student s2 = new Student (“saad” , 2); Student s3 = new Student (“raza” , 6);

// adding elements (Student objects) where roll nos // are stored as keys and student objects as values h.add(“one” , s1); h.add(“two” , s2); h.add(“six”, s3);

// checking whether hashmap is empty or not boolean b = h.isEmpty ();

if (b = = true) { System.out.println(“hashmap is empty”);

} else {

int size = h.size(); System.out.println(“hashmap size: ” + size); }

// retrieving student object against rollno two and // performing downcasting Student s = (Student) h.get(“two”);

// calling student’s class print method s.print();

} // end main

} // end class

Web Design & Development CS-

Output

--------------------