Object Oriented Programming
Instructor Name:
Lecture-9
Today’s Lecture
Arrays in Java
foreach Loop
Grouping Objects
Making use of Library Classes
2
Arrays
What is an Array
An array is a container object that holds a fixed number of values of a single
type.
The length of an array is established when the array is created.
Here is the way to create an array in java
int[] array;
array=new int[10];
or
int array[]={1,2,3,4,5,6,7};
3
Arrays
Printing an Array
class arrayTest{
public static void main(String arg[]){
int a[]={1,2,3,4,5,6,7};
for(int i=0; i<[Link];i++)
[Link](a[i]);
}
}
OUTPUT
1
2
3
4
5
6
7
4
Arrays
Sorting Array Using Built-in Functions
import [Link];
class sortAttay{
public static void main(String arg[]){
int a[]={11,2,23,4,15,6,17};
for(int i=0; i<[Link];i++)
[Link](a[i]);
[Link](a);
for(int i=0; i<[Link];i++)
[Link](a[i]);
}
}
This will sort the array in ascending order.
5
Arrays
Limitations with Arrays
Elements can not be added dynamically.
Elements cannot be removed automatically
Array size must be define at start
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.
How to Use ArrayList
In order to use the ArrayList class in your projects, you must import this class
as follows:
import [Link];
Declaring an ArrayList
After importing the Java ArrayList class, it is time to declare an object of the
ArrayList class. The default way is:
ArrayList array_name = new ArrayList();
An array “array_name” of the ten size is created. The ArrayList default
constructor creates an array of ten size, by default. 9
ArrayList<T>
Key Points of ArrayList
A Java ArrayList is a dynamic array, an array that can grow after it is created.
The standard arrays are not dynamic i.e. once a standard array is created it
cannot grow. These are of fixed size and you cannot add or remove elements
after these are created whereas the ArrayList allows that.
ArrayList in Java gives fast iteration.
It also gives fast random access.
The ArrayList provides more powerful insertion than the standard arrays.
Search mechanism in ArrayList is better than standard arrays.
The ArrayList manipulation is slower as a lot of shifting occur.
Works with non-primitive data types only.
10
ArrayList<T>
Most Common Methods
Methods Description
Add Add an element at the end of collection
Clear Remove all element
Contain Return true if element is present otherwise false
Get Return element at specific index
indexOf Return the index of first occurrence
Remove Remove from speicific index
Size Size f arrayList
trimToSize Trim to capacity to current number of elements
11
ArrayList<T>
ArrayList Example
import [Link];
public class ArrayListDemo {
public static void main(String args[]) {
ArrayList<String> al = new ArrayList<String>();
[Link]("Initial size of al: " + [Link]());
[Link]("C");
[Link]("A");
[Link]("E");
[Link]("B");
[Link]("D");
[Link]("F");
[Link](1, "A2");
[Link]("Size of al after additions: " +
[Link]());
[Link]("Contents of al: " + al);
[Link]("F");
[Link](2);
[Link]("Size of al after deletions: " +
[Link]());
[Link]("Contents of al: " + al); } } 12
ArrayList<T>
ArrayList Example – Output
Initial size of al: 0
Size of al after additions: 7
Contents of al: [C, A2, A, E, B, D, F]
Size of al after deletions: 5
Contents of al: [C, A2, E, B, D]
13
Collection
Generic Vs Non Generic Collection
Java collection framework was non-generic before JDK 1.5. Since 1.5, it is
generic.
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.
Let's see the old non-generic example of creating java collection.
ArrayList al=new ArrayList();
Let's see the new generic example of creating java collection.
ArrayList<String> al=new ArrayList<String>();
In generic collection, we specify the type in angular braces. Now ArrayList is
forced to have only specified type of objects in it. If you try to add another
type of object, it gives compile time error. 14
Collection
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
Files= new ArrayList<>();
Called Diamond Notion
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:
for(ElementType element : collection){
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
foreach Loop – contains method
public void listMatching(String searchString)
for(String alname : al) {
if([Link](searchString)) {
[Link](alname);
17
18