Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 7
Web Design & Development CS-
Web Design & Development CS-
ArrayList
Web Design & Development CS-
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
Web Design & Development CS-
HashMap
Web Design & Development CS-
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
Web Design & Development CS-