You are on page 1of 16

CHAPTER 6: OBJECTS of

ARRAYS

PROGRAMMING 2
COMP 112
Introduction
2

● Arrays in Java can be created from user –defined classes or


from built-in classes, where the elements of the array are
objects of the specific class.
ü Object array can be created just like normal arrays in Java.
1: Built-in class: String[] st1=new String[10];
2: User-defined class: Student
s = new Student[7];
ü First declaration will create an array of (String) Built -in
class with size of 10 to store String Objects.
ü Second declaration will create an array of (Student) which is
a user-defined class with size 7 to store Student objects.

Programming 2 COMP 112 Spring 20-21


Array of user-defined class (Student)
3

We will use a class Student containing a single instance variable


marks. Following is the definition of this class.

class Student {
int marks;
}

An array of objects is created just like an array of primitive type data


items in the following way.
Student[] studentArray = new Student[7];
The above statement creates the array which can hold references to seven
Student objects. It doesn't create the Student objects themselves. They have
to be created separately using the constructor of the Student class. The
studentArray contains seven memory spaces in which the address of seven
Student objects may be stored.
Programming 2 COMP 112 Spring 20-21
Array of Student objects
4

ü The Student objects have to be instantiated using the


constructor of the Student class and their references should be
assigned to the array elements in the following way.
studentArray[0] = new Student();
ü To create 7 objects of Student, the following code is used:
for ( int i=0; i<studentArray.length; i++) {
studentArray[i]=new Student();
}
ü The above for loop creates seven Student objects and assigns
their reference to the array elements. Now, a statement like
the following would be valid.
studentArray[0].marks=100;
Programming 2 COMP 112 Spring 20-21
Array of Student objects (cont’d)
5

— To create and apply the array of student objects, we us


Main class as following:
Public class Mains{
public static void main(String[] args) {
Student[ ] studentArray = new
Student[7]; studentArray[0] = new
Student();
studentArray[1] = new Student();
studentArray[2] = new Student();
studentArray[0].marks = 99;
studentArray[1].marks = 70;
studentArray[2].marks = 88;
System.out.println(studentArray[0].marks); //output: 99
}
}
Programming 2 COMP 112 Spring 20-21
Sorting Algorithm
6

Selection sort algorithm:

Public void selectionSort(Student[] A){


for (int lastPlace = A.length-1; lastPlace > 0;
lastPlace--) {
int maxLoc = 0;
for (int j = 1; j <= lastPlace; j++) {
if (A[j].marks > A[maxLoc].marks) {
maxLoc = j;
}
}
Student temp = A[maxLoc];
A[maxLoc] = A[lastPlace];
A[lastPlace] = temp;
}
Programming 2 COMP 112 Spring 20-21
UML class diagram ( static view )
7

Programming 2 COMP 112 Spring 20-21


Collections : ArrayList – part1
8

— ArrayList in Java is used to store dynamically sized


collection of elements. Contrary to Arrays that are fixed in
size, an ArrayList grows its size automatically when new
elements are added to it.
— ArrayList is part of Java’s collection framework.

Programming 2 COMP 112 Spring 20-21


ArrayList - part 2
9

Main features:
1. An ArrayList is a re-sizable array, also called a dynamic array. It
grows its size to accommodate new elements and shrinks the
size when the elements are removed.
2. ArrayList internally uses an array to store the elements. Just
like arrays, It allows you to retrieve the elements by their index.
3. Java ArrayList allows duplicate and null values.
4. Java ArrayList is an ordered collection. It maintains the
insertion order of the elements.
5. You cannot create an ArrayList of primitive types like int, char
etc. You need to use boxed types (Wrapper classes) like
Integer, Character, Boolean etc.
Programming 2 COMP 112 Spring 20-21
ArrayList – part 3
10

— Creating an ArrayList and adding an element on it: This


example shows:
1. How to create an ArrayList using the ArrayList() constructor?
2. Add new elements to an ArrayList using the add() method?
import java.util.ArrayList;
public class CreateArrayListExample {
public static void main(String[] args) {
ArrayList<String> animals = new ArrayList<>();
// Adding new elements
animals.add("Lion");
animals.add("Tiger");
animals.add("Cat");
animals.add("Dog");

Programming 2 COMP 112 Spring 20-21


ArrayList – part 3 (Cont’d)
11

System.out.println(animals);
// Adding an element at a particular index in
an ArrayList
animals.add(2, "Elephant");
System.out.println(animals);
}
}
// # Output
[Lion, Tiger, Cat, Dog]
[Lion, Tiger, Elephant, Cat, Dog]

Programming 2 COMP 112 Spring 20-21


ArrayList – part 4
12

● Removing an element from the ArrayList:


1. animals.remove(2);
//will remove the "Cat" by index.
2. animals.remove("Cat");
//will remove the cat by content.
3. animals.clear();
//will remove all objects.

Programming 2 COMP 112 Spring 20-21


ArrayList – part 5
13

● Iteration over ArrayList:


1: By using for loop with index:
for(int i = 0; i < animals.size(); i++){
System.out.println(animals.get(i));
}

2: By using for each:


for(String x: animals) {
System.out.println(x);
}

Programming 2 COMP 112 Spring 20-21


ArrayList – part 6
14

Searching the objects inside ArrayList:


1: System.out.println("Does animals array
contain\"Cat\"? : " + animals.contains("Cat"));
// will return True if cat exists and False otherwise.
2: System.out.println("index Of \"Cat\" +
animals.indexOf("Cat"));
// will return the first occurrence index of “Cat” if the
object exists.
3: System.out.println("last Index Of \"Cat\": +
animals.lastIndexOf("Cat"));
// will return the last occurrence index of “Cat” if the object
exists.

Programming 2 COMP 112 Spring 20-21


ArrayList-part 7
15

Sorting objects inside the ArrayList:


import java.util.ArrayList;
import java.util.Collections;
…………
…………
Collections.sort(animals);
System.out.println(animals);

// output will be sorted list


[ Cat, Dog, Elephant, Lion, Tiger ]

Programming 2 COMP 112 Spring 20-21


Important Definitions
16

● Pseudocode: way of designing programs by writing drafts


in a combination of English and Java.
● Selection sort: A simple sorting algorithm that searches
for the smallest or largest element n times.
● Static context: The parts of a class that run without
reference to a specific instance of the class.
● Collection: A Java library class, like ArrayList, that
represents a group of objects.

Programming 2 COMP 112 Spring 20-21

You might also like