Download Collections, Array List - Web Design and Development - Lecture Slides and more Slides Web Design and Development in PDF only on Docsity! Web Design & Development Lecture 6 Docsity.com Collections Docsity.com Collection Messages Basic messages constructor() Creates a collection with no elements int size() Number of elements in the collection boolean add() Add a new reference/element at the end of the collection Returns true is the collection is modified. iterator() Returns an Iterator Object Docsity.com Additional Collection Messages Utilities Additional useful methods boolean isEmpty() boolean contains(Object o) Iterative search, uses equals() boolean remove(Object o) Iterative remove(), uses equals() Boolean addAll(Collection c) Docsity.com ArrayList Docsity.com Example TestArrayList. java //checking whether arraylist contains student objects 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); } // continue.. Docsity.com Example TestArrayList. java //using loop to iterate for (int i=0; i< al.size(); i++) { Student s = (Student) al.get(i); s.print(); } } //end of main } Docsity.com Compile & Execute
MEO O Ric PaC aL mc)
D:\examples\collections> javac TestArrayList. java
D:N\examples\collections?> java TestArrayList
arraylist size: 3
Student name:ali, roll no:l
Student name:saad, roll no:2
Student name:raza, roll no:3
Docsity.com
Example TestHashMap. java /* HashMap is used to store Student objects as value and rollnos as keys. We are using the same Student class which we build in our previous lectures */ import java.util.*; public class TestHashMap { public static void main (String args[]){ HashMap h = new HashMap(); Student s1 = new Student(“ali”, 1); Student s2 = new Student(“saad”, 2); Student s3 = new Student(“raza”, 6); h.put("one", s1 ); h.put("two", s2 ); h.put("six", s6 ); //continue.. Docsity.com Example TestHashMap. java boolean b = h.isEmpty(); if (b == true){ System.out.println("hashmap is empty"); } else { int size = h.size(); System.out.println("hashmap size:"+ size); } Student s = (Student) h.get("two"); s.print(); } //end of main } Docsity.com Compile & Execute
Pama Oat ico PAC Re cl
D:\examples\collections> javac TestHashMap. java
D:\examples\collections> java TestHashMap
hashmap size:3
Student name:saad, roll no:2?
pe Sues cos tect Lon !
4
Docsity.com
Problem Address Book Wants to store name, address, phone no of a Person Features Add Delete Search (on name) Exit (from application) The above listed features must be available to user in the form of JOptionPane based MENU. Docsity.com Approach for Solving Problem Step 1 Make a Person class with name, address, phone no attributed Step 2 Make a AddressBook class Use ArrayList to store Person’s Obejcts Write methods add, delete and Search Use JOptionPane methods for IO Step 3 Make a Test class (Driver Program) Build Menu using switch selection structure Call appropriate methods of AddressBook Docsity.com Lets Start Coding! Docsity.com