Object Oriented Programming Object Oriented Programming: Lecture-9 Instructor Name
Object Oriented Programming Object Oriented Programming: Lecture-9 Instructor Name
Instructor Name:
Lecture-9
Today’s Lecture
Arrays in Java
foreach Loop
Grouping Objects
2
Arrays
What is an Array
An array is a container object that holds a fixed number of values of a single
type.
int[] array;
array=new int[10];
or
int array[]={1,2,3,4,5,6,7};
3
Arrays
Printing an Array
class arrayTest{
5
Arrays
6
Collection in Java
Strength of Java
As we have seen great strength of java is its rich set of predefined classes that
you can reuse rather than “reinventing the Wheel” again and again
The classes are grouped into packages- named group of related classes and are
collectively referred to as java class library or java application programming
interface(API).
One of the API is Collection which is used to store the group of related
objects
A Collection object can store an arbitrary number of other objects.
Efficient method to store , organize and retrieve the data with out requiring
additional knowledge of how to store it.
Have the ability to dynamically change the size to accommodate to more
element
Reduce size when deleting element from collection.
7
Collection in Java
Collection Examples
Here are some examples of collection that are related to programming context.
Electronic calendars store event notes about appointments, meetings,
birthdays, and so on. New notes are added as future events are arranged, and
old notes are deleted as details of past events are no longer needed.
Libraries record details about the books and journals they own. The catalog
changes as new books are bought and old ones are put into storage or
discarded.
Universities maintain records of students. Each academic year adds new
records to the collection, while the records of those who have left are moved
to an archive collection. Listing subsets of the collection will be common: all
the students taking first-year CS or all the students due to graduate this year,
for instance.
8
ArrayList<T>
What is ArrayList
The ArrayList is a Java class that is inherited from (or extends)
the AbstractList class. The ArrayList class implements the List interface.
Declaring an ArrayList
After importing the Java ArrayList class, it is time to declare an object of the
ArrayList class. The default way is:
10
ArrayList<T>
11
ArrayList<T>
ArrayList Example
import java.util.ArrayList;
public class ArrayListDemo {
public static void main(String args[]) {
ArrayList<String> al = new ArrayList<String>();
System.out.println("Initial size of al: " + al.size());
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
al.add(1, "A2");
System.out.println("Size of al after additions: " +
al.size());
System.out.println("Contents of al: " + al);
al.remove("F");
al.remove(2);
System.out.println("Size of al after deletions: " +
al.size());
System.out.println("Contents of al: " + al); } } 12
ArrayList<T>
13
Collection
Java new generic collection allows you to have only one type of object in
collection. Now it is type safe so typecasting is not required at run time.
Diamond Notation
Files= new ArrayList<String>();
It is possible for the java compiler to infer the parameterize the type of object
being created from the type of variable being assigned to
15
foreach Loop
foreach Loop
A for-each loop is one way to perform a set of actions repeatedly on the items
in a collection
We can summarize the Java syntax and actions of a for-each loop in the
following pseudo-code:
loop body
A for-each loop has two parts: a loop header (the first line of the loop
statement) and a loop body following the header. The body contains those
statements that we wish to perform over and over again.
16
foreach Loop
if(alname.contains(searchString)) {
System.out.println(alname);
17
18